I want to prevent a form from being submitted and I found this answer.
How can I listen to the form submit event in javascript?
This is the code
var checkForm = document.getElementById("check");
if(checkForm.addEventListener) {
checkForm.addEventListener("submit", submitFunc, false);
}else if(checkForm.attachEvent) {
checkForm.attachEvent("onsubmit", submitFunc);
}
function submitFunc() {
document.querySelector("#check").addEventListener("submit", function(e) {
e.preventDefault();
});
}
This does not work, it calls the function on submit but it still submits the form. However, if I move this part out of the function it works and it stops the form from being submitted.
document.querySelector("#check").addEventListener("submit", function(e) {
e.preventDefault();
});
But it was to my understanding that this part should go inside the callback function, it also seems more "correct" for this relevant code to go inside the relevant function. How come it doesn't work inside the function? Do I need to inject something in to the callback function to get it to work?