Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Jquery / Javascript function scope dilemna with dynamic content.

Status
Not open for further replies.

SunnyByfleet

Technical User
Feb 24, 2003
146
0
0
GB
Hello,

I am currently learning Javascript and as a project am writing a lightbox using exist examples.

I found one such in the JQuery cookbook, which works well. However, I wanted to add thumbnails to the box, for multiple images. These would be generated on the fly, and have onclick events assigned to update the main image.

Rather than include the entire source code, I have whipped up a greatly simplified example:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en-us" lang="en-us">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Sample</title>
<script type="text/javascript" src="[URL unfurl="true"]http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>[/URL]
<script>

// Initialize.
function init_demo() {

	// Insert modal at end of </body>.
	
	// Look for modal links.
	$('.click').click(function() {

		// Check the href="..."
		// Nofollow.
        show(0);
		this.blur();
		return false;
	}
    );

    function show(nval)
    {
        		var newval = ++nval;
                if (newval === 4) {newval=1;}
                $('#bin').html($("<img src='"+nval+".gif' onclick='show(newval);' title='"+nval+"' />"));
	
    }
    


}

// Kick things off.
$(document).ready(function() {
	init_demo();
});
</script>

</head>
<body>
<div id="container">

<img src="n.gif" class="click" />
</div>
<div id="bin">
</div>
</body>
</html>

If you run the above, and click the "n" (this assumes you have the relevent images...), then the 1 image appears. However, if you click the 1 image, you get an error, because it can't find the SHOW function.

It seems apparant to me that the generated code is not in the same scope as the rest of the function. How do I access the code, or do I have to restructure it entirely? I figure I could just get rid of the init function and have everything global, but that goes against everything I have learnt so far.

Any pointers would be great.

Thanks
 
try replacing :

Code:
// Initialize.
function init_demo() {

    // Insert modal at end of </body>.
    
    // Look for modal links.
    $('.click').click(function() {

        // Check the href="..."
        // Nofollow.
        show(0);
        this.blur();
        return false;
    }
    );

    function show(nval)
    {
                var newval = ++nval;
                if (newval === 4) {newval=1;}
                $('#bin').html($("<img src='"+nval+".gif' onclick='show(newval);' title='"+nval+"' />"));
    
    }
}

with :

Code:
// Initialize.
function init_demo() {

    // Insert modal at end of </body>.
    
    // Look for modal links.
    $('.click').click(function() {

        // Check the href="..."
        // Nofollow.
        show(0);
        this.blur();
        return false;
    }
    );
}
function show(nval)
{
    var newval = ++nval;
    if (newval === 4) {newval=1;}
    $('#bin').html($("<img src='"+nval+".gif' onclick='show(newval);' title='"+nval+"' />"));   
    }

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Ah, I shrunk my example down too much. In the full code, init_demo does a lot more work, and stores data in an array. My problem is, if I take the show function outside of the init_demo function, I no longer have access to that array.

I need to either be able to access the array externally, or access another function externally.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top