I'm facing an issue with submitting a form using 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");
});
The code should display an alert()
and cancel submitting.
Can anyone identify what's causing the issue in my code?
Thank you in advance...