104

PHP code:

<a id="a$id" onclick="check($id,1)" href="javascript:void(0)"  class="black">Qualify</a>

I want to remove the onclick="check($id,1) so the link cannot be clicked or "check($id,1) won't be fired. How can I do it with JQuery?

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Steven
  • 21,552
  • 40
  • 100
  • 126

8 Answers8

250

Old Way (pre-1.7):

$("...").attr("onclick", "").unbind("click");

New Way (1.7+):

$("...").prop("onclick", null).off("click");

(Replace ... with the selector you need.)

// use the "[attr=value]" syntax to avoid syntax errors with special characters (like "$")
$('[id="a$id"]').prop('onclick',null).off('click');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<a id="a$id" onclick="alert('get rid of this')" href="javascript:void(0)"  class="black">Qualify</a>
lesyk
  • 3,723
  • 3
  • 22
  • 39
glmxndr
  • 42,138
  • 27
  • 90
  • 115
  • 28
    Shouldn't that be removeAttr('onclick')? – Roatin Marth Dec 22 '09 at 20:11
  • Yep, good point, might be cleaner, but I'm not sure it would be different from execution point of view. – glmxndr Dec 23 '09 at 08:05
  • 6
    With jQuery 1.7+ you can use on/off: http://stackoverflow.com/questions/209029/best-way-to-remove-an-event-handler-in-jquery – Lance Cleveland Jan 06 '12 at 18:04
  • The .attr('onclick', '') is so obvious I cannot believe I didn't think of it. I googled the solution :-P – Barry Carlyon Jan 15 '14 at 15:22
  • Note, the `a` portion of that selector isn't needed. – Kevin B Feb 20 '14 at 16:00
  • 7
    See first comment on removeAttr http://api.jquery.com/removeattr/ `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` – andyface Sep 08 '14 at 09:20
  • 2
    @andyface yes that is in the documentation, but I had to use `.prop()` for IE11 also. – onetwo12 Dec 15 '14 at 13:06
58

I know this is quite old, but when a lost stranger finds this question looking for an answer (like I did) then this is the best way to do it, instead of using removeAttr():

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

Citing jQuerys official doku:

"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"

acme
  • 13,318
  • 6
  • 65
  • 102
  • 1
    I found that this is the best how to remove onlick event when you have typed it like this. `sometext>/span>` The event is fully unbinded and works great – zdarsky.peter Dec 25 '12 at 21:21
  • 3
    I don't think this is the best way. http://jsfiddle.net/TN2wr/ vs http://jsfiddle.net/TN2wr/1/ – Gus Sep 24 '13 at 01:10
  • I am using a JQ older than 1.6 therefore $(element).attr('onclick', null); works for me. – metamagikum Oct 13 '13 at 15:07
23

Removing onclick property

Suppose you added your click event inline, like this :

<button id="myButton" onclick="alert('test')">Married</button>

Then, you can remove the event like this :

$("#myButton").prop('onclick', null); // Removes 'onclick' property if found

Removing click event listeners

Suppose you added your click event by defining an event listener, like this :

$("button").on("click", function() { alert("clicked!"); });

... or like this:

$("button").click(function() { alert("clicked!"); });

Then, you can remove the event like this :

$("#myButton").off('click');          // Removes other events if found

Removing both

If you're uncertain how your event has been added, you can combine both, like this :

$("#myButton").prop('onclick', null)  // Removes 'onclick' property if found
              .off('click');          // Removes other events if found
Community
  • 1
  • 1
John Slegers
  • 38,420
  • 17
  • 182
  • 152
17

if you are using jquery 1.7

$('html').off('click');

else

$('html').unbind('click');
Jaydeep Jadav
  • 776
  • 10
  • 26
10

It is very easy using removeAttr.

$(element).removeAttr("onclick");
5

After trying so hard with bind, unbind, on, off, click, attr, removeAttr, prop I made it work. So, I have the following scenario: In my html i have NOT attached any inline onclick handlers.

Then in my Javascript i used the following to add an inline onclick handler:

$(element).attr('onclick','myFunction()');

To remove this at a later point from Javascript I used the following:

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

This is the way it worked for me to bind and unbind click events dinamically in Javascript. Remember NOT to insert any inline onclick handler in your elements.

bboydflo
  • 771
  • 1
  • 12
  • 23
  • I like this, but why would the one be prop and the other attr? – Stachu Aug 22 '13 at 19:46
  • I am not sure, but the way I see this is that with attr you add/inject onclick handler with the corresponding myFunction() and the prop('onclick',null) removes the attribute, so no function will be called. This is the way it worked for me. And i could notice how the element had onclick handler via attr and how it was removed after prop. – bboydflo Aug 23 '13 at 08:16
  • 1
    For future reference, you should really be using `.on('click', myFunction)` (note that `myFunction` is **not** in quotes) and then, later, `.off('click')` – JDB still remembers Monica May 19 '15 at 17:38
2

What if onclick event is not directly on element, but from parent element? This should work:

$(".noclick").attr('onclick','').unbind('click');
$(".noclick").click(function(e){
    e.preventDefault();
    e.stopPropagation();
    return false;
});
Milan
  • 21
  • 1
  • Adding another click-listener (to prevent default, etc.) might add it to the end of the listener list and thus the other listeners would still be executed first. I'm not sure though if it is added first or last to the list. Anyway it is better to use 'off' as seen in other answers. – Frederic Leitenberger Apr 09 '19 at 09:10
0

Try this if you unbind the onclick event by ID Then use:

$('#youLinkID').attr('onclick','').unbind('click');

Try this if you unbind the onclick event by Class Then use:

$('.className').attr('onclick','').unbind('click');
talonmies
  • 67,081
  • 33
  • 170
  • 244
Vishnu Sharma
  • 1,191
  • 11
  • 10