How to Clear/Remove JavaScript Event Handler?

Viewed 103979

Given the following HTML fragment:

<form id="aspnetForm" onsubmit="alert('On Submit Run!'); return true;">

I need to remove/clear the handler for the onsubmit event and register my own using jQuery or any other flavor of JavaScript usage.

6 Answers

To do this without any libraries:

document.getElementById("aspnetForm").onsubmit = null;

With jQuery

$('#aspnetForm').unbind('submit');

And then proceed to add your own.

For jQuery, off() removes all event handlers added by jQuery from it.

$('#aspnetForm').off();

Calling .off() with no arguments removes all handlers attached to the elements.

Related