Reputation: 97661
There is an annoying website out there that foils attempts to "open in new tab" by using <div onclick=>
instead of <a href=>
. I've written a bookmarklet, using jQuery, that creates a wrapper <a>
tag and inserts it within the <div>
around its content.
How can I remove the original onclick
handler?
Upvotes: 3
Views: 8378
Reputation: 1537
element.removeAttribute('onclick');
That's it. Actually removes listener. The other methods do not-- it is still active. See DevTools. Screenshots below.
Leaving a defunct listener usually not a big deal, but if you have a lot of these they waste cpu and your performance will drop on your page or app.
Also, it's kind of ugly still hanging around there alive in your code like some kind of orphaned eunuch.
Kind of surprised this question has been out there almost 15 years and never a right answer... although last one had the right idea, except the bit about "untested" being an Answer lol.
Throwing this out there 'cause I'm sure somebody has bumped into this trying to actually answer that question, properly, and could cause problems if not actually done right.
Disabling the effect is not the same as removing the cause. Sometimes it is not enough.
Upvotes: 0
Reputation: 12639
In some circumstances setting onclick
to null
won't work, but this should always work:
document.getElementById("myId").setAttribute("onclick", "");
Upvotes: 1
Reputation: 1381
Not mentioned yet:
$('#myId')[0].onclick = null; // remove the inline onclick() event
Upvotes: 5
Reputation: 9269
Untested, but...
$("selector").removeAttr("onclick");
I guess you have already tried to
$("selector").unbind("click");
Upvotes: 1