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  |  Permalink  |  Print Page


You Might Also Be Interested In:




Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »