7

Surprised, I am encountering this weird issue while submitting form from JS.

Issue:

Consider a simple form submitted using two ways from a submit button and an anchor link

<form method="POST" action="page.html" name="foobar" id="test">
  <input type="text" />
  <input type="submit" />
</form>

<a href="#" onclick="document.getElementById('test').submit();">click me</a>

Function catching the submit event

document.getElementById('test').onsubmit = function() {
   // Same result with 
   //     * document.foobar.onsubmit
   //     * document.forms['foobar'].onsubmit

   alert('foobar');
   return false;
}

Now, when the form is submitted from clicking the submit button I get the alert, but not when clicking the link. Why is this doing so?

Fiddle Showing the Issue

Community
  • 1
  • 1
Starx
  • 77,474
  • 47
  • 185
  • 261

3 Answers3

13

To provide a reasonably definitive answer, the HTML Form Submission Algorithm item 5 states that a form only dispatches a submit event if it was not submitted by calling the submit method (which means it only dispatches a submit event if submitted by a button or other implicit method, e.g. pressing enter while focus is on an input type text element).

If no submit event is dispatched, then the submit handler won't be called.

That is different to the DOM 2 HTML specification, which said that the submit method should do what the submit button does.

So if you want to use script to submit a form, you should manually call the submit listener. If the listener was added using addEventListener, then you'll need to remember that and to call it since you can't discover it by inspecting the form (as suggested below).

If the listener is set inline, or added to the DOM onsubmit property, you can do something like:

<form onsubmit="return validate(this);" ...>
  ...
</form>
<button onclick="doSubmit()">submit form</button>

<script>
function doSubmit() {
  var form = document.forms[0];

  if (form.onsubmit) {
    var result = form.onsubmit.call(form);
  }

  if (result !== false) {
    form.submit();
  }
}
</script>

Life is tougher if you need to pass parameters or do other things.

Gerrat
  • 28,863
  • 9
  • 73
  • 101
RobG
  • 142,382
  • 31
  • 172
  • 209
1

Thats how form.submit behaves;

The form's onsubmit event handler (for example, onsubmit="return false;") will not be triggered when invoking this method from Gecko-based applications. In general, it is not guaranteed to be invoked by HTML user agents

Make onsubmit call a function and simply call that onclick as well.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • I came to know it does not get invoked. But, do you know any reason why this happens? Or, is it that __browsers are made that way__? – Starx Oct 10 '12 at 12:40
  • The event is designed to fire only when the user submits the form, not js – Alex K. Oct 10 '12 at 12:47
  • But isn't user also submitting the form by click on an anchor link and calling methods to submit the forms?? – Starx Oct 10 '12 at 12:48
  • No, they are clicking an anchor link that happens to call the .submit method – Alex K. Oct 10 '12 at 12:53
  • @AlexK—The [W3C HTML spec](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-76767676) says that the submit method "Submits the form. It performs the same action as a submit button.", so it seems reasonable to me that it would also cause the form's submit listener to be called, if not dispatch a submit event. However, that isn't what some (most?) browsers do. – RobG Oct 10 '12 at 13:08
  • @AlexK—note that in the HTML "living standard", [section 4.10.22.3 Form submission algorithm](http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#concept-form-submit), if a form is submitted by calling its submit method, the form is not validated, nor is a submit event dispatched so the submit handler is never called. – RobG Oct 10 '12 at 13:21
1

Instead of programatically submitting the form using the forms submit() function, you could manually construct and dispatch a submit event.

const buttonEl = document.getElementById('your-button');
const formEl = document.getElementById('your-form');

buttonEl.addEventListener('click', () => {
   const submitEvent = new Event('submit');
   formEl.dispatchEvent(submitEvent);
});

This means any other listener on the form submit event will fire:

formEl.addEventListener('submit', (submitEvent) => {
    console.log('The form submitted!');
});
elwyn
  • 10,360
  • 11
  • 42
  • 52