views:

65

answers:

4

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

+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
Nice, so what's the main danger of using this opposed to DE's addEvent?
Dr. Frankenstein
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
Thanks, nice concise answer with explanation too.
Dr. Frankenstein
A: 
document.getElementById('target').onclick=function() {
  functionName();
  return false;
}

I assume you meant "without onclick html attribute"

mplungjan
And have an href to get the pointer
mplungjan
A: 

Use the getElementById method to find the element, and set it's onclick event:

document.getElementById('target').onclick = functionName;
Guffa
it rewrites the current onclick handler, not adds a new one to it
galambalazs
Sometimes that is good enough :)
mplungjan
@galambalazs: Yes, that is actually what the OP asked for, not more, not less.
Guffa
@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
but see, the ultimate answer is John Resig's addEvent from 2005 which was abandoned even by Resig. :)
galambalazs
@galambalazs: So you are saying that the code is dangerous to use? Do you have any references for that?
Guffa
@Guffa another onclick before your code is enough.
galambalazs
@galambalazs: And that is dangerous how?
Guffa
@Guffa you overwrite an onclick handler that was attached for a reason, and you break some functionality as a result.
galambalazs
@galambalazs: That might be exactly the behaviour that is intended. Keeping the previous event handler might be just as dangerous.
Guffa
@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
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
:) 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