|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
javascript closure not working
Hi All
I tried the following code Code:
window.onload = function()
{
var list = ['abc', 'xyz'] ; // id's from links
for(var i = 0; i < list.length; i++ )
{
var id = list[i] ;
document.getElementById(list[i]).onclick = function() { alert("link with id=" + id) }
}
}
Code:
document.getElementById(list[i]).onclick = function() { alert("link with id=" + list[i]) }
So, when I click on the link with id 'abc' I get the alert showing "link with id=xyz" For some reason the closure only memorizes the last values! (and also doesn't remember list) Any ideas ? thnx in advance LuCa Last edited by Kor; 06-25-2008 at 11:05 AM. Reason: wrap the code [code][/code] |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|