Given this HTML to create a form:
<form name="search" id="myForm" onsubmit="return existingfunc()" method="post">
<input type="submit" value="Search" title="Search">
</form>
I can change the form's name via jQuery using $('#myForm').attr('name','newname')
.
Is it possible to change the form's onsubmit
function?
To just change the form onsubmit event you could do this:
$('#myForm').attr('onsubmit', 'return somethingElse()');
But you probably want to remove the form's onsubmit attribute then add a new event using jQuery (note: unbind()
will remove only event handlers attached by jQuery):
$('#myForm').removeAttr('onsubmit').submit(function(e) { /* your logic here */ });
You can remove an existing event using jQuery's unbind
function, eg:
$('#myForm').unbind('submit');
will remove all onsubmit
events from #myForm
. Then you can add a new event using normal jQuery:
$('#myForm').submit(function(e) { /* your logic here */ });
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