views:

4511

answers:

3

Greetings all - 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. Anyone know how to do this?

Thanks - Jordan

+2  A: 

With jQuery

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

And then proceed to add your own.

Peter Bailey
This did not work...
Then I'm not sure - it's the "jQuery way" to remove event listeners.
Peter Bailey
You can only unbind event listeners that were added with jQuery's `bind()` method.
Matthew Scharley
+1  A: 

To do this without any libraries:

document.getElementById("aspnetForm").onsubmit = undefined;
jiggy
This worked perfectly - thanks!
I may have spoke to soon - this does not appear to work on IE 6 or 7. It reports an error: "Not implemented".
I may have spoken to soon - this does not appear to work on IE 6 or 7. It reports an error: "Not implemented".
Hmm... does it work if you set it to null instead of undefined? Although undefined *should* be the correct setting, null may work.
R. Bemrose
Setting it to null works. Thanks!
+1  A: 

Try this, this is working for me:

$('#aspnetForm').removeAttr('onsubmit').submit(function() {   
    alert("My new submit function justexecuted!"); 
});

See this for more details.

Ryan