As I work on improving my JavaScript skills, I've been experimenting with submitting a form using AJAX without jQuery. However, I've encountered an issue where I can't seem to prevent form submission using addEventListener();
in JavaScript.
window.addEventListener('load', function(){
document.forms[0].addEventListener('submit', function(){
send();
return false;
}, false);
}, false);
No matter how I rearrange the code or try different methods like return send();
or returnValue=false;
, the form still submits and the page reloads. I can only prevent this by using return false;
within an inline event listener, but is that the best approach?
Any suggestions or thoughts on why addEventListener();
doesn't stop form submission as expected?