var form = document.forms[0]; form.addEventListener("submit", function(){ var email = form.elements['answer_13829'].value; if( email == '[email protected]') { alert('redirecting the user...'); window.location = 'xxxx'; return false; } });
I don't understand - it still submits the form. Can someone patch my code and make it work?
return false cancels the default submit action(stops the submission of form).
To add a click event listener on div tag using JavaScript, we can call the addEventListener method on the selected div. to add a div with a class. Then we write: const div = document.
addEventListener() to listen for form submit, and logs the current Event. timeStamp whenever that occurs, then prevents the default action of submitting the form.
You need to use the preventDefault()
method of the event object.
Note that neither addEventListener()
nor preventDefault()
are supported in IE <= 8.
var form = document.forms[0]; form.addEventListener("submit", function(evt){ var email = form.elements['answer_13829'].value; if( email == '[email protected]') { evt.preventDefault(); alert('redirecting the user...'); window.location = 'xxxx'; } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With