skip to Main Content

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 = true;
$("form.form-update").submit((e)=>{
    e.preventDefault();
    $("form.form-update input").each((key, element) => {
        if (element.value == "") {
            statusForm = false;
        }
    });
    if (statusForm == false) {
        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")?

[Problem solved] Problem was solved with combined the unbind() and submit() method. So the code became :

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

2

Answers


  1. do it like that

    // When "yes" button click
    $(".confirm-notification-yes").click(()=>{
    // Code to remove preventDefault() on $("form.form-update")
     $("form.form-update").unbind('submit').click();
     })
    
    Login or Signup to reply.
  2. 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();
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search