14

This is a weird question. We have some production links down on our intranet. Some rouge javascript is returning a "false" on all links on our intranet homepage.

We don't have access to the source to re-build the control and fix this javascript.

So as a temporary band-aid I want to move all the "onClick" events on the links using jquery

The links right now are like this:

<a href="https://www.mysite.com/" onclick="AddHit('Header: mysite.com', 'http://www.mysite.com'); return false;" title="Mysite Home">www.Mysite.com</a>

I want to write some jquery to get ride of the "onclick='AddHit..." code.

Nate
  • 2,286
  • 4
  • 33
  • 52

9 Answers9

29

You can try this:

$("a").removeAttr("onclick");

Edit Updating answer based on comments and jquery Docs:

Note: Removing an inline onclick event handler using .removeAttr() doesn't achieve the desired effect in Internet Explorer 6, 7, or 8. To avoid potential problems, use .prop() instead

Best way to do this:

$("a").prop("onclick", null);
Rondel
  • 4,351
  • 10
  • 35
  • 62
7

Remove the attribute from all links that have it:

$('a[onclick]').removeAttr('onclick');
3

According to JQuery documentation you should use .prop() to remove an inline onclick, as this avoids issues in Internet Explorer 6, 7, or 8.

Try this

$element.prop("onclick", null);

I needed to remove the onclick of an tag after the first click so here is what I did:

onclick="$(this).prop('onclick', null);"

Hope it helps

Jay
  • 81
  • 1
  • 6
  • 2
    So far this is the only correct answer. Using jQuery, neither `.removeAttr()` nor `.unbind()` will safely remove an inline `onclick` event handler in certain versions of Internet Explorer. See [`.removeAttr()`](http://api.jquery.com/removeAttr) – WynandB Feb 13 '13 at 23:28
2

You can do something like this;

$('a[onclick]').removeAttr('onclick');
Stefan
  • 5,468
  • 4
  • 21
  • 29
2
$(function(){
    $('a[onclick*="AddHit"]').removeAttr('onclick');
}

This will remove all of them but you want to make sure that you don't remove it from unwanted links. A lot of stuff hooks into the onclick attribute, if you have a class you can verify before removing or the contents of the onclick it would be better. Would hate to watch the whole site stop working because needed JS didn't get called. This will remove any onclicks with the AddHit function in the onclick to keep you safer.

Tony
  • 3,199
  • 1
  • 25
  • 46
2

This removes the onclick attribute for all links that has an onclick value that starts with "AddHit(" (leaving other links untouched).

$('a[onclick^="AddHit("]').removeAttr("onclick");
SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
Shawn Chin
  • 74,316
  • 17
  • 152
  • 184
0

Keep this in mind, if your 'onclick' attribute is ONLINE AND in a MODAL like (bootstrap)

like this : (in modal)

<a id="myDiv" href="#" onclick="doSomething();">Click here !</a>

-> Do mention to the <body> tag !

in jQuery (with body in first selector) :

$('body #myDiv').prop('onclick', null);

I searched for a few hours ...

0

Not tested, but I think this should work:

$(document).ready(function(){
    $('a').removeAttr("OnClick");
});

More info: http://api.jquery.com/removeAttr/

lumbric
  • 4,739
  • 4
  • 36
  • 44
  • 3
    From the same page: "Removing an inline `onclick` event handler using `.removeAttr()` doesn't achieve the desired effect in Internet Explorer 6, 7, or 8. To avoid potential problems, use `.prop()` instead: `$element.prop("onclick", null);`" – WynandB Feb 13 '13 at 23:20
-1

Use .unbind() method or off() to get rid of that.

Nikola K.
  • 6,813
  • 13
  • 27
  • 39
Alireza
  • 5,172
  • 10
  • 43
  • 109
  • I believe that will remove event handlers added via jQuery or addEventListener() but not an inline onClick event handler. – Jasper – Foreever Jan 24 '14 at 12:02