Cutter, I like this suggestion:
>> objA.currentI = intI; >> objA.onclick = function(){ alert( this.currentI ); }; This is actually how I deal with this same problem in Flash Action Script sometimes. However, in Flash I do it on objects, and in JS it always makes me nervous to try adding data to an object that doesn't inherently have that attribute. ....................... Ben Nadel www.bennadel.com -----Original Message----- From: Cutter (CFRelated) [mailto:[EMAIL PROTECTED] Sent: Thursday, April 20, 2006 1:00 PM To: CF-Talk Subject: Re: OT: Javascript help Asking your question of my javascipt list, the responses were as follows: Repsonse 1 **** Have you tried this? objA.onclick = new Function ("alert(" + intI + ")" ); Response 2 **** >for (var intI = 0 ; intI < 10 ; intI++){ >> var objA = document.createElement( "div" ); Place one "var objA;" before the loop then remove the "var" inside. Redeclaring a variable is a bad practice that will bite you in other languages. >> objA.onclick = function(){ alert( intI ); }; It's doing its job, it's alerting the value of intI, which is what you left it. The easiest solution is to add a custom property to the object to hold the current value of intI: objA.currentI = intI; objA.onclick = function(){ alert( this.currentI ); }; Response 3 **** >objA.onclick = new Function ("alert(" + intI + ")" ); This should work, but from what I understand new Function (like eval) must be compiled on the spot, so there's a (however slight) performance hit. http://userjs.org/help/tutorials/efficient-code Hope some of this helps Cutter Ben Nadel wrote: > Not really for CF, but though someone here could lend some insight.... > > 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! > > Please help!!! > ....................... > Ben Nadel > www.bennadel.com > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Message: http://www.houseoffusion.com/lists.cfm/link=i:4:238315 Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4 Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4 Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4 Donations & Support: http://www.houseoffusion.com/tiny.cfm/54