tags:

views:

74

answers:

6

I have several such anchor links as shown below.

<a href="http://google.com" onClick="unhook()">  Google </a>

I want to apply the onClick event to all anchor tags dynamically. Is it possible?

A: 

Yes, put it inside a loop or add a class to all anchors and bind the click event to that function:

<a class='something' href="http://google.com" onClick="unhook()">  Google </a>
<script>
    $(".something").click(function(){
        unhook();

    });
</script>

Here I have used jQuery..don't forget to include jquery.js.

sathis
The question is not tagged with *jQuery*. At least mention that you are using jQuery.
Felix Kling
+3  A: 

This can be done fairly easily with jQuery:

$('a').bind('click', unhook);

This code selects all a tags and binds the unhook function to the click event.

And even shorter (thanks KennyTM):

$('a').click(unhook);
Oded
`$('a').click(unhook)`
KennyTM
@KennyTM - Thanks :)
Oded
+3  A: 

Using vanilla javascript:

function onclickHandler() {
  //do stuff
}

window.onload=function() {
  var aTags=document.getElementsByTagName('A');
  for (var i=aTags.length-1; i>=0; i--) {
    if(aTags[i].onclick) {
      var oldOnClick = aTags[i].onclick;
      aTags[i].onclick = function() {
        onclickHandler.call(this);
        oldOnClick.call(this);
      }
    } else {
      aTags[i].onclick = onclickHandler;
    }
  }
}

Check here: http://jsfiddle.net/496af/1/ (Updated with the code edits.)

aularon
If there is an onclick clause already with the anchor tag then I do not want to change it. Can you explain how it can be done?
shantanuo
I edited it, check it now. consider using some library like `jquery` cause it makes it easy to deal with events.
aularon
+1 for not instantly jumping to jquery when it wasn't mentioned. for a one-off like this, jquery isn't necessary, as much as i love it. but i'm curious to know why you're looping backwards?
Mark
@Mark I'm doing it so I don't have to access `aTags.length` at each loop iteration, or use the alternative solution which involves defining a new variable to hold `aTags.length` value. (and since looping direction doesn't matter in this case).
aularon
`aTags.length` isn't costly is it? Thought it was no worse than just `i`.
Mark
@Mark, it's actually a bit costy, if you want you can put a question about it for other people interested, or I can answer you here.
aularon
@aularon: What, does it actually count the elements every time? Figured it would be stored internally and kept track of.
Mark
@mark, no it doesn't get recounted, but accessed, which seems to delay things a tiny little while, assigning `aTags.length` to a variable makes it faster.
aularon
@aularon: I'm surprised you aren't using `--i` too then.
Mark
@Mark, I will try to remember that more often, thanks :)
aularon
A: 

If you really want to add the click handler to all links, you can access them with document.anchors:

var anchors = document.anchors;
for(var i = 0, l = anchors.length;i < l;i++) {
    anchors[i].onclick = unhook;
}

I use traditional event handling here as it should work in all browsers. There are other, more advanced ways to add handlers, but it differs a bit between IE and other browers (but of course it is not impossible ;)).

Felix Kling
A: 

What you can do is wrap all these A in a DIV and set the click event once ont that DIV and not on all A.

<div onclick="unhook(event||window.event);return false;">
    <a href="http://google.com"&gt;  Google </a>
    <a href="http://apple.com"&gt;  Apple </a>
</div>

<script>
    function unhook(e){
        var elm = e.target || e.srcElement;
        //do the unhook, ie:
        elm.parentNode.removeChild(elm);
    }
</script>
Mic
A: 

You can do that using jQuery

1.

$('a').click( function() {
   // your unHook code here
} );

Or 2.

$('a').click( unHook() );

or if you don't want to use jQuery, then simply you can use.

allAnchors = document.getElementsByTagName ( 'a' );
for ( i = 0 ; i < allAnchors.length; i++ ){
   allAnchors[i].onClick = unHook;
}
Rakesh Juyal
for 2 I think you mean `$('a').click(unHook)` unless `unHook()` returns a function.
Matt Ellen