Prevent one jQuery event with another

To remove my jQuery, I am looking to create a β€œconfirm” class for various elements with event handlers so that I can launch a quick confirmation window so that the user can confirm their action.

If the user clicks on cancel in the confirmation window, any subsequent events should be canceled, but this should not be permanent, for example, triggering the event again should cause confirmation and start again. If I can fix this, I can take some delicate action with a confirmation question to make sure the user wants to click.

For instance:

$('.confirm').click(function() {
    if(!confirm('Are you sure?')) {
        // Stop any other click events on $(this) from happening this instance.
    }
});

$('.clicker').click(function() {
    alert('it happened');
});

<a class="clicker confirm">Click me</a>

The desired conclusion will be that canceling the confirmation will cancel the click event, but will not completely disable it.

+5
2

event.stopImmediatePropagation() jQuery:

$('.confirm').click(function(event) {
    if(!confirm('Are you sure?')) {
        event.stopImmediatePropagation();

        // Stop any other click events on $(this) from happening this instance.
    }
});

$('.clicker').click(function() {
    alert('it happened');
});

jsFiddle

+5
$('.confirm').click(function() {     
return confirm('Are you sure?');
});

event.stopPropagation() click, .

0

All Articles