[jquery] jQuery delete confirmation box

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....

This question is related to jquery

The answer is


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


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.


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();

});

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");
  });
});

I used this:

<a href="url/to/delete.asp" onclick="return confirm(' you want to delete?');">Delete</a>

Simply works as:

$("a. close").live("click",function(event){
     return confirm("Do you want to delete?");
});

Try this my friend

_x000D_
_x000D_
  // Confirmation Message On Delete Button._x000D_
_x000D_
  $('.close').click(function() {_x000D_
    return confirm('Are You Sure ?')_x000D_
  });
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<td class='close'></td>
_x000D_
_x000D_
_x000D_


$(document).ready(function(){
  $(".del").click(function(){
    if (!confirm("Do you want to delete")){
      return false;
    }
  });
});