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?
onclick="document.getElementById('test').submit();"
withonclick="document.getElementById('test').onsubmit();"
– Coats