You do not need to use form.submit()
ever. Do it properly (onsubmit
), or use click()
on the submit button.
Doing it properly...
I can't think of a good reason to automatically submit a visible form. To submit data without user-interaction use XMLHttpRequest or WebSockets.
A form is submitted by user interaction (e.g. pressing its submit button), so there is no need to use JavaScript to submit a form. It is more likely that you need JavaScript to prevent a form submission, by returning false
in the onsubmit
event handler.
...or use click()
To programatically invoke HTML5 validation (and also any JavaScript onsubmit
event handlers attached to the form), you can call the click()
function of a submit button that belongs to the form.
If the form has no submit button, you can create a temporary one:
var form = document.getElementById("mc-embedded-subscribe-form");
var button = document.createElement('input');
button.type = 'submit';
button.style.display = 'none';
form.appendChild(button);
button.click();
Forms with multiple submit buttons should each have name
attributes so the server can detect which button the user clicked. You can 'click' these buttons using form.buttonName.click()
.