1

In my code's else condition I want to remove preventDefault() and submit the form.

I don't know to do this?

if(email.val() != ""){
            //check email
            e.preventDefault();
            $.ajax({
                type: 'post',
                url: '/register/check-email',
                data: {email: email.val()},
                success: (data) => {
                    console.log(data);
                    if(data == 1){
                        name.css('border', '1px solid #248ea9');
                        email.css('border', '1px solid red');
                        $("div.wrapper div.right div.form form small.error-name").hide();
                        $("div.wrapper div.right div.form form small.error-email").hide();
                        $("div.wrapper div.right div.form form small.error-email-found").show();
                    }else{
                        //remove preventDefault()
                    }
                }
            })
        }
7
  • preventDefault() will not run in the else block since you haven't put it in there. Or rather, none of the code in the first set of { } will run if your else block of code fires. It's one or the other. You can't remove something thats not there to begin with. Commented Dec 13, 2019 at 15:24
  • 2
    It's asynchronous, you can't do it Commented Dec 13, 2019 at 15:25
  • @AlonEitan $.ajax() is an asynchronous task, but why should it not be possible to submit the form in the else branch? Commented Dec 13, 2019 at 15:28
  • @Andreas Because the OP is trying to prevent something that is depended on the response from the server Commented Dec 13, 2019 at 15:30
  • 1
    @Saad this should be the form, hence this.submit() would also work, hence no need to query the DOM again and no additional jQuery object. Commented Dec 13, 2019 at 15:31

3 Answers 3

2

An answer is never too late :)

My workaround is to add e.stopPropagation() at the end of the code. This somewhat resets the form to its initial state.

if(email.val() != ""){
            //check email
            e.preventDefault();
            $.ajax({
                type: 'post',
                url: '/register/check-email',
                data: {email: email.val()},
                success: (data) => {
                    console.log(data);
                    if(data == 1){
                        name.css('border', '1px solid #248ea9');
                        email.css('border', '1px solid red');
                        $("div.wrapper div.right div.form form small.error-name").hide();
                        $("div.wrapper div.right div.form form small.error-email").hide();
                        $("div.wrapper div.right div.form form small.error-email-found").show();
                    }else{
e.stopPropagation();
                        //remove preventDefault()
                    }
                }
            })
        }
Sign up to request clarification or add additional context in comments.

Comments

1

I guess you executed e.preventDefault () in the button of a form, this will prevent the submit of the form, if you want to continue to submit you can use the submit() method of the form directly, like this:

if(email.val() != ""){
            //check email
            e.preventDefault();
            $.ajax({
                type: 'post',
                url: '/register/check-email',
                data: {email: email.val()},
                success: (data) => {
                    console.log(data);
                    if(data == 1){
                        name.css('border', '1px solid #248ea9');
                        email.css('border', '1px solid red');
                        $("div.wrapper div.right div.form form small.error-name").hide();
                        $("div.wrapper div.right div.form form small.error-email").hide();
                        $("div.wrapper div.right div.form form small.error-email-found").show();
                    }else{
                        //remove preventDefault()
                        // get form and submit
                        $("#form").submit()
                    }
                }
            })
        }

Comments

0

it dose not matter where do you put the e.preventDefault() at the or at the bottom it will work the same.

if you have the action on the form you can put it right after you start the block of submit function.

var submit = function (e) {
    e.preventDefault()
}

i don't know about your case very much but you can use other methods like:

e. stopPropagation();

e.stopImmediatePropagation();

for further read : https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault

3 Comments

It's inside an ajax callback, of course it's important where he put it!
as far as i see you did not you put it before making request. and i'm telling you to put it before if statement. out side of the if block. and explain what do you want to do what is your default action is it the form ? i mean tell me why do you need the e.preventDefault at the first place.
But the OP specifically ask about a case where he want to put in inside the ajax success callback - This is what the question is about

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.