Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// 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();
    }
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading