473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

assigning events dynamically

dear esteemed javascript gurus.

consider the following fragment:

var buttons =
document.getElementById("tr_Toolbar").getElementsB yTagName("button");
for (var i=0; i<buttons.length; i++) {
var button = buttons[i];
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

Nov 8 '06 #1
3 1447
aj****@blueyonder.co.uk wrote:
var buttons =
document.getElementById("tr_Toolbar").getElementsB yTagName("button");
for (var i=0; i<buttons.length; i++) {
var button = buttons[i];
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
Nov 8 '06 #2
ASM
aj****@blueyonder.co.uk a écrit :
>
consider the following fragment:

var buttons =
document.getElementById("tr_Toolbar").getElementsB yTagName("button");
for (var i=0; i<buttons.length; i++) {
var button = buttons[i];
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.

--
ASM
Nov 8 '06 #3
<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 :-)

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

Mike
Nov 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Thomas | last post by:
Hi, I'm having a problem with the dynamically created inputfields in Internet Explorer. The situation is the following: - I have a dynamically created table with a textbox in each Cell. - It...
4
by: blue | last post by:
I have a drop-down list, a radio button list and a submit button. I'm adding these controls to a table and I'm adding the table to a Placeholder. I'm adding it to the Placeholder because I don't...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
1
by: Earl Teigrob | last post by:
PROBLEM: When a user control is loaded into a PlaceHolder control more than once, the events do not fire on the first click of a control on the dynamically loaded user control. In other words, the...
5
by: karthick raja | last post by:
Am experiencing a problem intercepting the events from controls added dynamically to a Placeholder control at runtime. Is there any way that I can write an event handler on the page which will be...
1
by: Paul | last post by:
An early Happy Thanksgiving to you and yours... We develop data driven apps. We dynamically place controls on a panel which may be then be placed on a web form or another control. My problem is...
9
by: Erik Frey | last post by:
Hi there, Just curious as to whether there's a clever way to see the events a control/object is firing off, perhaps written out to the debug console. It would be really handy to know which...
7
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written...
2
by: Nathan Sokalski | last post by:
I have LinkButtons that are dynamically created in one of the PostBack events. They must be created in the PostBack event because one of the variables required to determine which ones to create...
4
by: webgour | last post by:
Hello, I'm having difficulty using attachEvent instead of simply assigning to mouseover in my object Sample03. When i use myImage.onmouseover = this.showmouseover(); in the following all works...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.