Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I remove inline event handlers using jQuery?

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.

like image 492
Skilldrick Avatar asked Oct 02 '10 15:10

Skilldrick


People also ask

Which of the following method can be used to remove event handlers?

The off() method is most often used to remove event handlers attached with the on() method.

Which event is removed in jQuery?

jQuery unbind() Method The unbind() method removes event handlers from selected elements.

Which event handler method would be used to remove an event handler that was attached with on function?

The . off() method removes event handlers that were attached with . on() .

Which jQuery method is used to attach a handler to an event?

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.


2 Answers

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.

like image 72
Darin Dimitrov Avatar answered Oct 06 '22 19:10

Darin Dimitrov


$("#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. ;-)

like image 23
Tomalak Avatar answered Oct 06 '22 18:10

Tomalak