user2266370
user2266370

Reputation: 41

Use jQuery to change text on a link on first press, then go to link on second press

I'm trying to use jQuery to make a "Are you sure?" function on a delete link. My current code:

$('.confirm-delete').on('click', function(e) {
e.preventDefault();
$(this).removeClass("confirm-delete");
$(this).html("<i class='icon-remove icon-white'></i> Are you sure?");
$(this).attr("href", "?delete="+this.id.replace('id', ''));
});

I have given the link the class "confirm-delete" and it changes both the text and the url fine, but it will not let me press a second time. Nothing happens. I suspect it's because of the preventDefault, that's why I try to remove the class, but to no effect. I hope someone can help me out with this. Thank you.

Upvotes: 0

Views: 56

Answers (1)

techfoobar
techfoobar

Reputation: 66693

You can do this by unbinding the click event handler right after the first run:

$('.confirm-delete').on('click', function(e) {
    e.preventDefault();
    $(this).removeClass("confirm-delete");
    $(this).html("<i class='icon-remove icon-white'></i> Are you sure?");
    $(this).attr("href", "?delete="+this.id.replace('id', ''));

    // unbind
    $(this).off('click');
});

Upvotes: 2

Related Questions