PDA

View Full Version : binding value-passing event handlers to a collection of elements


brothercake
03-24-2003, 07:01 PM
I want to bind event-handlers to each item in a complex list. What I'm doing at the moment is such a hack I'm almost embarrased to post it .... this:

tree = document.getElementById('list');
data = tree.innerHTML;
data = data.replace(/<li/ig,'<li onmouseover="openMenu(this,false)"');
tree.innerHTML = data;

I told you it was a hack ;) But it works .. that much is certain.

Is there a better way? I can't see how elm.onevent or addEventListener could be used, because they can't pass values. What else can I do?

I need an x-browser solution, by which I mean moz, safari/konqi, O7 and IE5. I don't mind if it takes a different approach for each one ... or even if there's a better way for the others and I still have to use this hack for IE.

Any improvement would be welcome; this method is a big onload process overhead I could well do without.

liorean
03-25-2003, 12:11 AM
Yes they can...

do
element.onevent=function(){fnSomething(this,that);return(false||true);}
instead of
element.onevent=fnSomething;

'this' will be a reference to the element in question.



I think you forget that the html attribute value string acts as the block of a javascript function - so, the contents of the attribute value equates to the contents of the function block in JavaScript.

brothercake
03-25-2003, 12:17 AM
Oh .. yes ... thanks for the reminder :) Presumably from that I can extract the event as this.event ?

jkd
03-25-2003, 12:23 AM
If you're not going to explicitly pass the event object (why not?), you can always go

arguments.callee.caller.arguments[0]

brothercake
03-25-2003, 12:29 AM
Originally posted by jkd
If you're not going to explicitly pass the event object (why not?), you can always go

arguments.callee.caller.arguments[0]
Oh, as in fnSomething(event) ...