How to stop and then start / run an event using jQuery?

I am trying to stop the default action when clicking a link. Then I ask for confirmation and, if I confirm, I want to continue the event. How can I do it? I can stop the event, but I can not start it. Here is what I still have:

$(document).ready(function(){
  $(".del").click(function(event) {
    event.preventDefault();
    if (confirm('Are you sure to delete this?')) {
      if (event.isDefaultPrevented()) {
        //let the event fire. how?
      }
    }
  });
});
+5
source share
2 answers

There is no need to disable default startup. Just do the following:

$(function() {
  $(".del").click(function(evt) {
    if (!confirm("Are you sure you want to delete this?")) {
      evt.preventDefault();
    }
  });
});

It is easier and more logical to prevent an event if you need to, rather than prevent it, and then prevent it (if possible).

Remember that the code will stop working when a confirmation window is displayed to the user until the user selects OK or Cancel.

, JavaScript: event.preventDefault() vs return false. , , stopPropagation(), return false:

$(function() {
  $(".del").click(function(evt) {
    if (!confirm("Are you sure you want to delete this?")) {
      return false;
    }
  });
});
+4

confirm()

$(function() {
  $(".del").click(function() {
    return confirm("Are you sure you want to delete this?");
  });
});
+2

All Articles