0

I am using the following for a delete button and although I get the confirm message, if I click cancel it still goes through with it:

<a onclick="return confirm('are you sure?')" class="btn btn-default btn-sm deleteList" href="#" list_id="1467" title="Delete your list: listName">Delete</a>

Anything obvious?

UPDATE:

This is attached to a jquery click function, so if the cancel stops the link following through, how can I prevent the click handler from firing?

UPDATE 2

Also tried at start of click handler but same problem:

$('.deleteList').on('click', function(e) {
        confirm('are you sure?');
        var list_id = $(this).attr("list_id");
        $.ajax({
            context: this,
            type: "POST",
            url: '/ajax/actions/deleteList.php?listId=' + list_id,
            success: function (data) {
                    var d = $.now();
                    window.location.href = "/pages/dashboard?listDelete=" + d + "#mylists";
            }
        }); // End .ajax
        e.preventDefault();
    });
crush
  • 16,713
  • 9
  • 59
  • 100
StudioTime
  • 22,603
  • 38
  • 120
  • 207

1 Answers1

2

Here is the fool-proof answer:

$('.deleteList').on('click', function( event ) {
    event.preventDefault()
    event.stopImmediatePropagation(); //de Added by @crush
    return confirm("are you sure?")
})

Then remove onclick="...".

As of the updated question, the entire question is different. The issue is now an invalid use of confirm() and a possible asynchronous problem with $.ajax().

digitalextremist
  • 5,952
  • 3
  • 43
  • 62