I have the following:
<script type="text/javascript">
function CancelFormButton(button) {
$(button.form).submit();
}
</script>
<form onsubmit="alert('here');">
<input type="button" value="Cancel" onClick="CancelFormButton(this);" />
</form>
When I click the "Cancel" button, the onsubmit from the form tag is not triggered.
This line instead submits the form successfully: $(button.form).submit();
but skips the alert('here');
within the onsubmit in the form tag.
Is this correct or am I doing something wrong?
By the way, in this case, I want this functionality, but I'm just wondering if I'm going to run into a problem in a browser where the onsubmit is triggered.
My simple solution:
It is work for me.
The easiest solution to workaround this is to create 'temporary' input with type submit and trigger click:
Sorry, misunderstood your question.
According to Javascript - capturing onsubmit when calling form.submit():
(emphasis added).
Note: "activated by a user" also includes hitting submit buttons (probably including default submit behaviour from the enter key but I haven't tried this). Nor, I believe, does it get triggered if you (with code) click a submit button.
Instead of
$("form").submit()
try this
trigger('submit') does not work.beacuse onSubmit method does not get fired. the following code works for me, call onSubmit method when using this code :
$("form").find(':submit').click();
This work around will fix the issue found by @Cletus.
Will work on all modern browsers.
Will work across tabs/spawned child windows (yes, even in IE<9).
And is in vanilla!
Just pass it a DOM reference to a form element and it'll make sure all the attached listeners, the onsubmit, and (if its not prevented by then) finally, submit the form.