Reputation: 1822
I have a form I'm trying to stop users from double-submitting using javascript. For some reason,even when the handler function returns false and event.preventDefault are called, the form will still submit multiple times. Anyone know why? Also how do I make this work as intended?
document.getElementById('my_form').addEventListener("submit", function(evt){
if ( validationFunction(document.getElementById('submit_button')) == false) {
evt.preventDefault();
return false;
}
});
Function validationFunction(){ return false }
<form method="post" id="my_form">
<input name='textbox'>
<input type="submit" id="submit_button">
</form>
Upvotes: 0
Views: 68
Reputation: 800
document.getElementById('my_form').addEventListener("submit", function(evt){
if ( validationFunction(document.getElementById('submit_button')) == false) {
// anything here
evt.preventDefault();
return false;
}
// anything here
event.preventDefault();
});
Upvotes: 1
Reputation: 2815
Try it in simple way. Like this
HTML
<form id="my_form">
<input name='textbox'>
<input type="button" id="submit_btn">
</form>
JS
document.getElementById("submit_btn").addEventListener("click", function(){
document.getElementById("my_form").submit(); // if validation is true run this
});
Upvotes: 1