Consider I have the following HTML:
<form id="myForm" action="/echo/html/" method="post">
<input type="text" value="10" />
<input type="submit" value="submit" />
</form>
<input type="submit" form="myForm" value="submit" />
<input id="myInput" type="text" form="myForm" />
and with that HTML I have the following JS:
document.addEventListener('submit', function(e) {
console.log(e, 'addEventListener');
});
document.getElementById('myInput').addEventListener('keyup', function(e) {
console.log(e.target.form);
e.target.form.submit();
});
(Note: this code has been optimized to work in jsfiddle and thus uses /echo/html/. The docs on this are here)
There are three parts to the above code:
- The
<form>(id="myForm"), which has atext inputand asubmit input. - The
submit inputoutsidemyForm, but, using the attributeform="myForm"will submit the data insidemyForm. - The
text inputwhich when registers akeyup, will firemyFormby usinge.target.form.submit(). This does not, however, fire thesubmitevent attached to the document.
My question is:
How do I simulate/trigger/etc. a
submitevent, which actually fires asubmitevent which is currently being listened by thedocument, using the 3rd method I describe above (on akeyupfor atext input).
P.S. I could not get a snippet working as it was erroring in stackoverflow, so the jsfiddle is here.