Skip to content Skip to sidebar Skip to footer

How To Pass/escape An Argument To An Event Handler Attached Using Javascript?

In the code HTML+Script below, an event handler is attached after page load using setAttribute(...). The event handler is passed an argument which is read dynamically too. the eve

Solution 1:

Do not use setAttribute to set event handlers on DOM nodes. Just use the event handler property direct, and assign it a function rather than a string:

document.getElementById('myId').onmouseover = function() {
  showMessage(document.getElementById('otherId').innerHTML);
};

Solution 2:

Tim's post up there helped me figure it out; albeit my understanding is empirical.

In addition to making a direct assignment

e.g.

document.getElementById(...).onMouseOver = stuff;
function stuff(){//do the Deed};

in lieu of

document.getElementById(...).setAttribute(...);

Apparently the event handler attached may not accept any argument.

I changed my handler signatures to accept 0 arguments, and it works!


Post a Comment for "How To Pass/escape An Argument To An Event Handler Attached Using Javascript?"