passing arguments to dynamic event handlers
say i create a event handler for some element:
document.getElementById('foo').onclick = bar;
which works fine, but say bar() needs to know who called it (foo)
document.getElementById('foo').onclick = bar('foo'); // no workie
document.getElementById('foo').onclick = bar(this); // no workie
document.getElementById('foo').onclick = "bar(this)"; // no workie
document.getElementById('foo').onclick = "bar('foo')"; // no workie
and what i found on google is basically you cannot directly pass arguments to a function reference. the problem is, i can't indirectly pass them either.
So basically here's my question, is there any way for my functyion bar() to determine what html elment invoked it without that element having to explicitly send a 'this' reference?
druagord posted this at 15:20 — 16th December 2003.
He has: 335 posts
Joined: May 2003
when assigning the event handler you cannot pass parameter try it like this
document.getElementById('foo').onclick = bar;
yes depending on the browser you can get who called it
function bar(eventparam)
{
if(!eventparam)
{
eventparam=window.event; // for IE
}
callername = eventparam.target.name;
}
this code is not tested bt should help you start
IF , ELSE , WHILE isn't that what life is all about
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.