10

i have a problem when submitting form with javascript submit()

LIVE ACTION : https://jsfiddle.net/98sm3f3t/

HTML :

<form id="myForm" action="">
    First name: <input type="text" name="fname"><br>
    <button id="myButton" type="button">Submit form</button>
</form>

JS :

document.getElementById("myButton").addEventListener("click", function() {

    document.getElementById("myForm").submit();

});

document.getElementById("myForm").addEventListener("submit", function(e) {

    e.preventDefault();

    alert("cancel submitting");

});

it supossed to be showing alert() and cancel submitting.

what's wrong with my code?

thanks in advance...

2
  • stackoverflow.com/questions/645555/… Commented Jun 8, 2016 at 14:23
  • If your intent is to manually submit the form, then you don't need a handler on the form. Just make a function with the necessary code, and invoke it when you're ready in the handler on your button. Commented Jun 8, 2016 at 14:32

7 Answers 7

14

Per MDN (emphasis added):

[The HTMLFormElement: submit() method] is similar, but not identical to, activating a form's submit . When invoking this method directly, however:

  • No submit event is raised. In particular, the form's onsubmit event handler is not run.
  • Constraint validation is not triggered.

Instead, you can use the requestSubmit() method, which submits a form as if the submit button had been clicked (i.e. it will run the onsubmit handler).

Sign up to request clarification or add additional context in comments.

Comments

9

Submitting a form programmatically, using JavaScript, does not trigger a submit event.

If you want your code to show an alert and not submit the form, then write it so it just shows an alert.

document.getElementById("myButton").addEventListener("click", function() {
    alert("Your message");
});

Alternatively, use a submit button to submit the form (instead of JavaScript).

Comments

5

Fix to your problem is to use input of type submit:

<button id="myButton" type="submit">

https://jsfiddle.net/joherro3/98sm3f3t/4/

For this to work you have to remove the click event listener which is submiting your form. Listen to form submit and call preventDefault() is enought to prevent form to be submit.

1 Comment

Yes! Sorry @Quentin I added information to the answer.
1

I just came over this and found the correct answer.

To trigger an event programatically in JavaScript you can do something like this.

create a submit event that bubbles up and cannot be canceled

const form = document.getElementById('pform');

const evt = new Event("submit", {"bubbles":true, "cancelable":false});
// event can be dispatched from any element, not only the document
form.dispatchEvent(evt);

https://developer.mozilla.org/en-US/docs/Web/API/Event/Event

Comments

0

change

<button id="myButton" type="button">Submit form</button>

to

<button id="myButton" type="submit">Submit form</button>

1 Comment

This doesn't work. Clicking the submit button triggers the click event and submits the form normally. The normal submission gets cancelled and triggers the alert. The programmatic submission still submits the form.
0

You are using button type ="button".

Use button type="submit".

It will alert

jsfiddle

1 Comment

This doesn't work (even in your live demo). Clicking the submit button triggers the click event and submits the form normally. The normal submission gets cancelled and triggers the alert. The programmatic submission still submits the form.
0

solution to trigger event programatically

`const form = document.getElementById('myform');

const evt = new Event("submit", {"bubbles":true, "cancelable":false});

form.dispatchEvent(evt);`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.