Jquery prevent default not working

Posted 3 months ago by pickab00

So I have a form submission which I am preventing the default. So the code looks something like this.

$("#form").submit(function(e){

var allowSubmit = false;

if (!allowSubmit){
    e.preventDefault();
}

//run something here and say

var a = 1;

if (a != 1) {
    allowSubmit = false;
    console.log('failed');
}else{
    allowSubmit = true;
    console.log('works');
}

});

I don't see the problem with this code. The e.preventDefault(); works just fine. But console logs 'works' and it is supposed to set the allowSubmit variable to true correct? Then why is my form not being submitted now that the allowSubmit is true and preventDefault is no longer true?

Please sign in or create an account to participate in this conversation.