Sorry. I think I made a mistake by suggesting return false
. The preventDefault
approach is what you need. I tested this out with the following example: https://www.kirupa.com/html5/examples/contactform_test.htm
The code for both the form and the JavaScript looks as follows:
<form id="contactForm" method="POST">
<div>
<p>Name</p>
<input name="name" type="text"> <br> </div>
<div>
<p>E-Mail (Optional)</p>
<input name="email" type="text">
<br>
</div>
<div>
<p>Comment</p>
<textarea cols="30" name="comment" rows="9"></textarea>
<br> </div>
<div>
<input name="submit" type="submit" value="Send!"> </div>
</form>
<script>
var theForm = document.querySelector("#contactForm");
theForm.addEventListener("submit", handleSubmit, false);
function handleSubmit(e) {
e.preventDefault();
}
</script>
Notice that I listen for the submit event on the form and then call preventDefault
in the event handler for that event. Let me know if you have any questions.
Cheers,
Kirupa