[Javascript] adding onclick event's with YUI
Matt McKeon
matt at mysticalmonkey.com
Wed Jul 18 11:21:16 CDT 2007
I'm using YUI to add onclick events to a list of links unobtrusively. I
can do it if I manually set each one up, but if I do the same thing in a
loop it doesn't quite work as expected.
What I'm trying looks like this:
var me = this;
function callbackClick1(e) { EVT.preventDefault(e); me.derp(1), me; }
EVT.on(DOM.get('item-21').firstChild, 'click', callbackClick1);
derp: function(index) {
alert(index);
}
The code above works as I expect, but in a loop through the list I use a
counter variable in place of the hardcoded number above and every link
when clicked alert()'s the last number in the loop (in this case 3). But
if I do each of them manually it works fine.
I've set up a test page which you can see in action and hopefully
understand what I'm try to say here: <http://qurl.com/ts66b>
I'm thinking it has something to do with scope, but I can't pindown the
problem. Anyone have any ideas?
Thanks,
Matt
More information about the Javascript
mailing list