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...
form. Just make a function with the necessary code, and invoke it when you're ready in the handler on yourbutton.