Is this what you were going for?

<html>
        <body>
                <div id="mainContainer"></div>
        </body>
</html>

<script>
        for (IntI = 0; IntI < 10; IntI++)
        {
                divCont = document.createElement('DIV');
                divCont.id = 'div' + IntI;
                divCont.appendChild(document.createElement('BR'));
                divCont.style.background='ececec';
                divCont.style.borderColor = '000000';
                divCont.style.borderStyle = 'solid';
                divCont.style.borderWidth = '1px';
                divCont.setAttribute('onClick', 'alert(' + IntI + ')');
        
document.getElementById('mainContainer').appendChild(divCont);
        
document.getElementById('mainContainer').appendChild(document.createElement(
'BR'));
        }       
</script>

The onclick of the divs wont work in IE 6-


..:.:.:.:.:.:.:.:.:.:.:.:.
Bobby Hartsfield
http://acoderslife.com

 

 


-----Original Message-----
From: Ben Nadel [mailto:[EMAIL PROTECTED] 
Sent: Thursday, April 20, 2006 9:35 AM
To: CF-Talk
Subject: OT: Javascript help

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:238289
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

Reply via email to