How to remove preventDefault()?

I want to deactivate the form temporarily and display a confirmation box whether the data wants to be sent or not, but I have problems when I want to activate the form again.

Code to deactivate form :

var statusForm = false;
$("form.form-update").submit((e)=>{
    e.preventDefault();
    $("form.form-update input").each((key, element) => {
        if (element.value == "") {
            statusForm = true;
        }
    });
    if (statusForm == true) {
        alert("Data is invalid!");
    }
    else{
        // Show the popup
        $(".confirm-notification").show();
    }
})

Code when "yes" button is clicked :

// When "yes" button click
$(".confirm-notification-yes").click(()=>{
    // Code to remove preventDefault() on $("form.form-update")
})

How to remove preventDefault() on $("form.form-update")?

>Solution :

On "Yes", submit the form:

$(".confirm-notification-yes").click(()=>{
    $("form.form-update").submit();
})

Now, let’s fix the first part. I suppose submission is started through a button. You put your code in that button, not the submit event.

$("btn-that-initiates-submission").click(() => {
    e.preventDefault();
    let missingValue = false;
    $("form.form-update input").each((key, element) => {
        if (element.value === "") {
            missingValue = true;
        }
    });
    if (missingValue) {
        alert("There's missing data in the form.");
    }
    else {
        $("confirm-notification").show();
    }
});

Leave a Reply