jQuery - remove onclick from an element
-
20-06-2021 - |
Domanda
I have the following setup:
<div id="whatever" onclick="dostuff()">Super Mega Giga Button or Whatever</div>
<!-- Import jquery and whatever other libs I need-->
<script>
$("#whatever").click(function() { //do something });
</script>
Now what I need to do is at some point to remove all the onclick events.
I tried $("#whatever").unblind('click'); but this removes only the event added with jquery, the dostuff() function is still called.
Any idea how can I remove all at once?
P.S. Don't start asking why would some1 have inline onclick and also listeners.
Soluzione
You can use the removeAttr()
method:
Remove an attribute from each element in the set of matched elements.
$('#whatever').removeAttr('onclick')
$("#whatever").click(function() {
//do something
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow