Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return false on addEventListener submit still submits the form?

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?

like image 949
David Avatar asked Feb 07 '11 16:02

David


People also ask

What is return false in form submit?

return false cancels the default submit action(stops the submission of form).

Can you add an Eventlistener to a div?

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.

What is form addEventListener?

addEventListener() to listen for form submit, and logs the current Event. timeStamp whenever that occurs, then prevents the default action of submitting the form.


1 Answers

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';   } }); 
like image 142
Tim Down Avatar answered Oct 01 '22 20:10

Tim Down