So I want an element i.e: <a id="target">click me</a>
to perform a JavaScript function i.e:
functionName();
I want to avoid using the "on" attributes i.e: onclick=""
.
By cross-browser I mean the A-grade browser compatibility list from YUI
views:
65answers:
4
+3
A:
var el = document.getElementById("target");
var old = el.onclick;
el.onclick = function() {
if(old) old();
functionName();
// if you want to prevent default action
// return false;
};
For a more robust solution, see: Dean Edward's addEvent.
galambalazs
2010-07-06 13:49:37
Nice, so what's the main danger of using this opposed to DE's addEvent?
Dr. Frankenstein
2010-07-06 15:05:49
there is no danger, it's just not including event bubbling, or the ability to remove events. So for simple things this will do, but if you want to do some advanced event handling, Dean's script offers a complete solution.
galambalazs
2010-07-06 15:28:02
Thanks, nice concise answer with explanation too.
Dr. Frankenstein
2010-07-06 15:38:23
A:
document.getElementById('target').onclick=function() {
functionName();
return false;
}
I assume you meant "without onclick html attribute"
mplungjan
2010-07-06 13:50:06
A:
Use the getElementById
method to find the element, and set it's onclick event:
document.getElementById('target').onclick = functionName;
Guffa
2010-07-06 13:50:06
@galambalazs: Yes, that is actually what the OP asked for, not more, not less.
Guffa
2010-07-06 14:20:34
@Guffa and if he asks for a knife to commit suicide? You hand it to him eagerly? I may consider convincing him not to do things that he may regret. :)
galambalazs
2010-07-06 14:25:49
but see, the ultimate answer is John Resig's addEvent from 2005 which was abandoned even by Resig. :)
galambalazs
2010-07-06 14:36:19
@galambalazs: So you are saying that the code is dangerous to use? Do you have any references for that?
Guffa
2010-07-06 14:37:10
@Guffa you overwrite an onclick handler that was attached for a reason, and you break some functionality as a result.
galambalazs
2010-07-06 15:25:33
@galambalazs: That might be exactly the behaviour that is intended. Keeping the previous event handler might be just as dangerous.
Guffa
2010-07-06 15:34:29
@Guffa And **might** is the world that is emphasized here. The concept of event handlers involves adding and removing handlers on a target. So a script that attached the handler should be able to call it or remove if not needed. And no script should possess absolute power over an event.
galambalazs
2010-07-06 15:47:08
A:
I don't know if this counts as a library, but here are the addEvent() and removeEvent() functions written by John Resig (yes, that John Resig):
function addEvent( obj, type, fn )
{
if (obj.addEventListener)
{
obj.addEventListener( type, fn, false );
}
else if (obj.attachEvent)
{
obj["e"+type+fn] = fn;
obj[type+fn] = function() { obj["e"+type+fn]( window.event ); };
obj.attachEvent( "on"+type, obj[type+fn] );
}
}
function removeEvent( obj, type, fn )
{
if (obj.removeEventListener)
{
obj.removeEventListener( type, fn, false );
}
else if (obj.detachEvent)
{
obj.detachEvent( "on"+type, obj[type+fn] );
obj[type+fn] = null;
obj["e"+type+fn] = null;
}
}
Use:
addEvent(document.getElementById('target'), 'click', functionName);
removeEvent(document.getElementById('target'), 'click', functionName);
Ryan Kinal
2010-07-06 13:55:52
:) omg, that John Resig? I jeez in my pants.... Please note that even Resig is using Dean Edward's event handler in jQuery. Let's read the related thread: http://stackoverflow.com/questions/3185513/event-handling-in-ie/
galambalazs
2010-07-06 14:34:28