Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my form not submit when the submit button is named "submit"?

Tags:

jquery

forms

I need to check if the form is submitted by the user and then perform some operation before subsequently submitting the form programmatically.

So I have this code:

$('form').submit(function (e) {
    e.originalEvent && e.preventDefault();

     //I change some things in the form here.

    !e.isTrigger && $(this).submit();
}); 

This works fine, but when the form submit button is named "submit" it does not.

It even works ok if the button is named "Submit". (uppercase "S")

I'm just curious as to why this is?

Here is a demo fiddle

like image 558
andrew Avatar asked Jan 03 '15 15:01

andrew


People also ask

Why My submit button is not working?

Sometimes the problem is caused by old versions of the Javascript files, cached by your browser and can be fixed by clearing the browser cache. You can use the browser console of your browser for debugging. After the Javascript error is fixed, the submit button will automatically be enabled.

How do I make my submit button work?

The <input type="submit"> defines a submit button which submits all form values to a form-handler. The form-handler is typically a server page with a script for processing the input data. The form-handler is specified in the form's action attribute.

What triggers a 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.

How do we define the submit button in a form?

The Submit Button The <input type="submit"> defines a button for submitting the form data to a form-handler. The form-handler is typically a file on the server with a script for processing input data. The form-handler is specified in the form's action attribute.


1 Answers

You fell prey to some ancient DOM quirks. Namely, forms in the DOM get all their input elements (and buttons, too) exposed as properties by their respective name:

<form>
  <input name="foo">
</form>

results in:

var form = document.forms[0];
form.foo === document.getElementsByName('foo')[0];

which is unfortunate, when you have any control with the name submit, because it shadows your original form's submit method.

Edit: By the way, for reading on many of those pitfalls I suggest to take a look at Kangax' fantastic DOMLint documentation.

like image 51
Boldewyn Avatar answered Sep 30 '22 04:09

Boldewyn