assigning events dynamically

A

ajfish

dear esteemed javascript gurus.

consider the following fragment:

var buttons =
document.getElementById("tr_Toolbar").getElementsByTagName("button");
for (var i=0; i<buttons.length; i++) {
var button = buttons;
button.onmouseup = function() { mouseup(button.id); return false; }
}

during this loop, button.id is correctly returned as the id of the
button being processed, and all the buttons are assigned a mouseup
event. however, whichever button is clicked, the mouseup function is
always invoked with the id of the last button in the list.

I guess it's because the "button.id" in the function definition is
being evaluated at the wrong time.

I have found that in my particular case I can use "this.id" instead.
but can someone explain how "button.id" is evaluated in the context of
the code fragment above.

TIA

Andy
 
M

Michael Winter

var buttons =
document.getElementById("tr_Toolbar").getElementsByTagName("button");
for (var i=0; i<buttons.length; i++) {
var button = buttons;
button.onmouseup = function() { mouseup(button.id); return false; }
}
[snip]

I have found that in my particular case I can use "this.id" instead.
but can someone explain how "button.id" is evaluated in the context of
the code fragment above.


In short, each function references the same scope chain, therefore each
function object shares the same values, including any changes, for each
property in that chain.

This issue has been discussed several times in the past. Search the
group archives for the words, "closure" and "loop".

Mike
 
A

ASM

(e-mail address removed) a écrit :
consider the following fragment:

var buttons =
document.getElementById("tr_Toolbar").getElementsByTagName("button");
for (var i=0; i<buttons.length; i++) {
var button = buttons;
button.onmouseup = function() { mouseup(button.id); return false; }
}

during this loop, button.id is correctly returned as the id of the
button being processed, and all the buttons are assigned a mouseup
event. however, whichever button is clicked, the mouseup function is
always invoked with the id of the last button in the list.


Because on each passage the previous variable 'button' is assigned to a
new element still last of the list.
So in fact 'button' is definitively this last element, everywhere it is
called.
I have found that in my particular case I can use "this.id" instead.

When event is fired (onmouseup) in an element 'this' is this element and
'this.id' is its id.
 
A

ajfish

<snip>

thanks mike - found the posts. when my brain is up to the job I'll try
and go back to make sense of them :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top