40

In my jQuery am displaying my results in a table formatted output, a part of my jQuery is

<td class='close' onclick='deleteItem(#ITEMID,#ITEMNAME)'></td>

here "close" is a CSS class and in this class I have cross mark image, if I click this cross mark the function(deleteItem) will be fired.

here what I want to do is if I click this cross mark a "delete confirmation box" should pop up and if I click yes this onclick event should fire, else if I click No nothing should happen.

How can I achieve this, can anyone help me....

shanish
  • 1,964
  • 12
  • 35
  • 62

9 Answers9

121

You need to add confirm() to your deleteItem();

function deleteItem() {
    if (confirm("Are you sure?")) {
        // your deletion code
    }
    return false;
}
Māris Kiseļovs
  • 16,957
  • 5
  • 41
  • 48
  • 6
    This works, however it should be noted that this is not jQuery-specific and is just vanilla JavaScript. – crmpicco May 28 '13 at 11:20
32
$(document).ready(function(){
  $(".del").click(function(){
    if (!confirm("Do you want to delete")){
      return false;
    }
  });
});
Kobi
  • 135,331
  • 41
  • 252
  • 292
Shashank Hegde
  • 2,533
  • 1
  • 18
  • 18
16

I used this:

<a href="url/to/delete.asp" onclick="return confirm(' you want to delete?');">Delete</a>
Umair Hamid
  • 3,509
  • 3
  • 23
  • 25
11

Try with below code:

$('.close').click(function(){
var checkstr =  confirm('are you sure you want to delete this?');
if(checkstr == true){
  // do your code
}else{
return false;
}
});

OR

function deleteItem(){
    var checkstr =  confirm('are you sure you want to delete this?');
    if(checkstr == true){
      // do your code
    }else{
    return false;
    }
  }

This may work for you..

Thanks.

Chandresh M
  • 3,808
  • 1
  • 24
  • 48
8
function deleteItem(this) {
    if (confirm("Are you sure?")) {
          $(this).remove();
    }
    return false;
}

You can also use jquery modalin same way

JQuery version

Are you sure?
  $(document).ready(function() {
    $("#dialog-box").dialog({
      autoOpen: false,
      modal: true
    });

  $(".close").click(function(e) {
    var currentElem = $(this);
    $("#dialog-box").dialog({
      buttons : {
        "Confirm" : function() {
          currentElem.remove()
        },
        "Cancel" : function() {
          $(this).dialog("close");
        }
      }
    });

    $("#dialog-box").dialog("open");
  });
});
Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73
4

Try with this JSFiddle DEMO : http://jsfiddle.net/2yEtK/3/

Jquery Code:

$("a.removeRecord").live("click",function(event){
   event.stopPropagation();
   if(confirm("Do you want to delete?")) {
    this.click;
       alert("Ok");
   }
   else
   {
       alert("Cancel");
   }       
   event.preventDefault();

});
Rony SP
  • 2,618
  • 15
  • 16
3

Simply works as:

$("a. close").live("click",function(event){
     return confirm("Do you want to delete?");
});
readikus
  • 369
  • 1
  • 6
  • 17
3

Try this my friend

  // Confirmation Message On Delete Button.

  $('.close').click(function() {
    return confirm('Are You Sure ?')
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td class='close'></td>
Wael Assaf
  • 1,233
  • 14
  • 24
2

Update JQuery for version 1.9.1 link for deletion is here $("#div1").find('button').click(function(){...}