I have some HTML with an onclick
attribute. I want to override that attribute using jQuery. Is there any way to remove the click handler using jQuery? Using unbind doesn't work.
The off() method is most often used to remove event handlers attached with the on() method.
jQuery unbind() Method The unbind() method removes event handlers from selected elements.
The . off() method removes event handlers that were attached with . on() .
Commonly Used jQuery Event Methods The click() method attaches an event handler function to an HTML element. The function is executed when the user clicks on the HTML element.
Try the .removeAttr()
function:
$(function() {
$('a').removeAttr('onclick');
});
jsfiddle demo.
Once you've got rid of the onclick
attribute you could attach your own click handler.
$("#theElement")[0].onclick = function() {
whatever();
};
// -- or --
$("#theElement")[0].onclick = null;
It's not that jQuery would make the standard DOM functions and properties go away. ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With