0

Do we have any event like "onBeforeSomeEvent" in jQuery?

For Ex,

<input type='button' class='confirmdelete' onclick='delete();' value='DELETE' />

Now I want to show a custom dialog for confirmation. (I don't want to use default confirm method in JavaScript).

So I need something like,

$('.confirmdelete').onBeforeClick(function() {
     if(true) //I show the custom dialog here and check some conditions. 
        return true;
     return false;
});

The onclick event should fire if I get 'true' from the onbeforeclick event.

Sorry, If I am confusing. May be like beforeActivate in Accordion.

arulmr
  • 8,620
  • 9
  • 54
  • 69
Pitchai P
  • 1,317
  • 10
  • 22

5 Answers5

1

Why don't you just wrap the delete()?

Put your validation on the onclick() event, and if it's true, delete it from there.

Dragos Bobolea
  • 772
  • 7
  • 16
0

Maybe, .mousedown() might help you.

Francis
  • 198
  • 1
  • 10
0

Add unique id to your delete buttons.

Try using .click() functionality of jQuery and jQuery UI dialog.

$('.confirmdelete').click(function () {
    var cur_id = this.id;
    var buttons = '<div id="dialog' + cur_id + '" title="Confirm" align="center"><input type="button" value="Yes" id="yes' + cur_id + '" /><input type="button" value="No" id="no' + cur_id + '" /></div>'

    $(this).after(buttons);
    $("#dialog" + cur_id).dialog();

    $("#yes" + cur_id).click(function(){
        alert('true - ' + cur_id);
        //Write your delete code here
        $(this).parents(".ui-dialog-content").dialog('close');
        $("#dialog" + cur_id).remove();
    });

    $("#no" + cur_id).click(function(){
        $(this).parents(".ui-dialog-content").dialog('close');
        $("#dialog" + cur_id).remove();
    });
});

Replace the alert with your delete code in the commented section. Here is a example: JSFIDDLE

BenMorel
  • 34,448
  • 50
  • 182
  • 322
arulmr
  • 8,620
  • 9
  • 54
  • 69
0

This link may help you jQuery delete confirmation box

There also first trigger the confirmation for delete and after that event is executed.

Community
  • 1
  • 1
Padmaja
  • 13
  • 7
0

You can do this:

HTML

<input type='button' class='confirmdelete' value='DELETE' />

JS

$('.confirmdelete').click(function () {
    var del = confirm('Are you sure?');
    if (del) {
        delete();  // Just call your "delete()" method here..
        return true;
    } else {
        return false;
    }
});
palaѕн
  • 72,112
  • 17
  • 116
  • 136