Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should jQuery's $(form).submit(); not trigger onSubmit within the form tag?

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.

like image 285
Darryl Hein Avatar asked Mar 14 '09 06:03

Darryl Hein


People also ask

What triggers form submit?

The submit event fires when the user clicks a submit button ( <button> or <input type="submit">) or presses Enter while editing a field (e.g. <input type="text">) in a form. The event is not sent to the form when calling the form. submit() method directly.

What does Onsubmit in form do?

The onsubmit attribute provides the script datas to executed whenever the submit event is occurred so it does not submit the datas to the server with the help of submit() function it will be postponed to the server side.

Why form submit is not working in jquery?

The NUMBER ONE error is having ANYTHING with the reserved word submit as ID or NAME in your form. If you plan to call . submit() on the form AND the form has submit as id or name on any form element, then you need to rename that form element, since the form's submit method/handler is shadowed by the name/id attribute.


1 Answers

Sorry, misunderstood your question.

According to Javascript - capturing onsubmit when calling form.submit():

I was recently asked: "Why doesn't the form.onsubmit event get fired when I submit my form using javascript?"

The answer: Current browsers do not adhere to this part of the html specification. The event only fires when it is activated by a user - and does not fire when activated by code.

(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.

like image 144
cletus Avatar answered Nov 10 '22 09:11

cletus