Dereferencing Javascript Variables in Dynamic Methods

Posted April 19, 2006 at 10:46 PM

Tags: Javascript / DHTML

There is one problem in Javascript that I cannot seem to get a handle on and it is killling me! I can't seem to get variables to pass by value as I would hope. Take the following example:

for (var intI = 0 ; intI < 10 ; intI++){
var objA = document.createElement( "div" );

// Set the click for the link.
objA.onclick = function(){ alert( intI ); };

// Set the link into the body div.
objDiv.appendChild( objA );
}

Now, in my head, each one of those links, when clicked should alert the appropriate intI value (0, 1, 2, 3, etc.); however, each of them will alert 10 which is the value that broke the FOR loop. It's like they all point to one variable and then get updated for each loop of the FOR iteration.

I can't seem to find a good solution to this. One method that seems to work, but is poop is something along the lines of:

// Define a function INSIDE this function.
function GetI( intX ){
return(
function(){ alert(intX); };
);
}

for (var intI = 0 ; intI < 10 ; intI++){
var objA = document.createElement( "div" );

// Set the click for the link.
objA.onclick = GetI( intX );

// Set the link into the body div.
objDiv.appendChild( objA );
}

This method works as would be expected, though I seem to think that it is doing the exact same thing. It must be something to do with the scoping. Since the intI value is getting passed to a local scope (int GetI()), and then getting passed back, it must be unique (since the local scope of the GetI() method is created unique of each FOR iteration.

This solution seems truly ganky to me. There has to be a better way. And this is just a simple example. I have many places where I want to be doing this with object reference and dynamic event handling. This one simple bumb is really holding me back!

Post Comment  |  Ask Ben  |  Print Page


You Might Also Be Interested In:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 18, 2010 at 6:59 AM
Using A Name Suffix In ColdFusion's CFMail Tag
Thanks Ben. This is really a very helpful article. Keep posting... Congrats. ... read »
Mar 18, 2010 at 6:03 AM
ColdFusion Session Management Revisited... User vs. Spider III
Hi Ben, no, the timour is not dynamic at all. The two code chunks are: // This application definition is for robots that do NOT need sessions. this.name = "eClipse_KABS_Boomerang" ... read »
Mar 18, 2010 at 1:25 AM
Ask Ben: Blocking WSDL Access In A ColdFusion Application
Well,I have to say,this is too complicated for me to understand.But I think you're great,keep doing it. ... read »
Mar 17, 2010 at 11:36 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
@Ben, It is. But I have tested again and sleep() seem to be keeping windows hanging. As a result, multiple people were having issues uploading and removing images when they are trying to do the imag ... read »
Mar 17, 2010 at 11:22 PM
Google Maps Not Working in Internet Explorer (IE)
@James The page has a problem in IE7, because line 112 has a comma at the end. Line 112 is the first problem, there are many more with the same. Ralph ... read »
Mar 17, 2010 at 8:39 PM
Looping Over ColdFusion JSON Queries In jQuery
Thanks Ben - this is just what I needed - just getting up to speed with jQuery - and your posts are accelerating that process substantially :) ... read »
Mar 17, 2010 at 7:50 PM
ColdFusion ArraySplice() Method As An Example Of A Dynamic Method Signature
Java is awesome and I love seeing new twists on it. @Ben - I totally agree with you. ... read »
Mar 17, 2010 at 4:13 PM
Testing For NULL Values In A ColdFusion Query Result Set
To get around the empty strings in an UPDATE statement I pretended the variable was a string by enclosing it in single quotes, CAST it to an integer, let it convert empty strings to zeroes, and used ... read »