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.
views:
47answers:
2
+3
A:
Try the .removeAttr() function:
$(function() {
$('a').removeAttr('onclick');
});
Once you've got rid of the onclick attribute you could attach your own click handler.
Darin Dimitrov
2010-10-02 15:03:20
Perfect! Thanks.
Skilldrick
2010-10-02 15:05:27
+4
A:
$("#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. ;-)
Tomalak
2010-10-02 15:03:40
@Tomalak - you're right, but the nice thing about jQuery functions is that they apply to all the elements, so I can remove onclick from all matching elements without having to loo over them.
Skilldrick
2010-10-02 15:34:41
@Skilldrick: Technically, `removeAttr()` is a loop, too. But I agree it's more convenient that writing the loop yourself.
Tomalak
2010-10-02 15:45:08