Below body contains nine textfields and onload 'SetListener' is called to attach eventlistener for event 'onpropertychange'. and trying to alert the name of the textfield.
but below code alert always 'Txt9' as its the last value.
My requirement is to pass the corresponding name.
I'm not looking for event.srcElement.name which I knows.
This is a specific requirement,ie, Pass name to event listener within loop.
1. You may use a self reference, this
2. attachEvent() is an IE only method. In case you don't want to add new functions to a handler, you may use the crossbrowser simple DOM 0 syntax :
element.onevent=function(){somefunction()}
3. onpropertychange is an IE only event. I guess that for your needs, the crossbrowser onchange would be enough
there is no global created here, there are no anonymous functions, and you should never use eval for something like this. IE had enough closures and leaks to keep track off without extra eval overhead...
In fact, you should probably not use eval at all until you've been writing javascript for at least 2 years, otherwise it's virtually guaranteed to be abused as a workaround for not understanding something inherit to JavaScript itself.
There are very few tasks below dynamic code generation and/or re-interpretation that demand eval().
"There are no anonymous functions here" when you have : x=function(){whatever} its an anonymous function.. Sorry--look at the definition. And it you don't have the wit to use eval(), take your own advice and don't use it until 2011, at the latest, if ever.
"There are no anonymous functions here" when you have : x=function(){whatever} its an anonymous function..
anonymous means it has no name, but more importantly to computer science, that it has no identifier. probably arguing over semantics here...
x=function(){whatever} is not an anon function, it's reachable by x after all.
it's just a function statement assign to the variable x, as compared to the named function expressions you see a lot more of.
your event handlers would also be reachable through the event model, and thus not really anonymous either.
in js, we typically use anon functions to create lambda expressions, or to provide a private scope.
note that for convenience, javascript allows any function (anon or named) to have an internal name, which is only always recognized by code inside the function:
example:
Code:
var junk=(function myFunc(){
alert(typeof myFunc);
}());
Bookmarks