IF I have a button-less form and I want to test if the possible onsubmit function returns true and then submit it. This is my current code, which works fine.
var form = document.getElementById('form');
var evt = document.createEvent('Event');
evt.initEvent('submit', true, true);
if(form.dispatchEvent(evt))
{
form.submit();
}
Isn't it possible to make dispatchEvent also submit the form?
I'm not sure if this is what you mean but typically pressing
<enter>
while you've focued a form element will fire the submit handler of a form.Programatically firing events is very buggy.
Actually, you can submit a form programmatically just like you want.
Example: http://jsfiddle.net/9fF6e/8/
MDN: https://developer.mozilla.org/en-US/docs/Web/Guide/DOM/Events/Creating_and_triggering_events#Triggering_built-in_events
This works for Firefox, Chrome, Safari and other modern browsers.