views:

52

answers:

5

I am using jQuery's .remove() to get rid of divs of content, but I would like to be able to have a confirmation or dialog box appear before it is removed. I am just not sure of the context of how I would write such a dialog.

$(".remove").click(function () {
  $('div').remove('#item1');
});

Upon clicking the link with class remove I would like a box to popup saying Are you sure you want to remove this? Then click Yes to remove and no to keep it. Thanks in Advance

+3  A: 

Try this:

$(".remove").click(function () {
  if(confirm("are you sure you want to remove the div")){
    $('div').remove('#item1');
  }
});

Hope it helps

sTodorov
+3  A: 
$(".remove").click(function () {
    if (confirm('Are you sure?')) {
        $('div').remove('#item1');
    }
});
Coronatus
+9  A: 
$(".remove").click(function () {
  if(confirm("Are you sure you want to remove this?")) {
    $('div').remove('#item1');
  }
});
Marc Gravell
Perfect thanks!
CarterMan
+2  A: 

Try the window.confirm: https://developer.mozilla.org/en/window.confirm

$(".remove").click(function () {
  if(window.confirm('Are you sure?')) {
    $('div').remove('#item1');
  }
});
David
+1  A: 

I think that if you are looking for a nicer confirm dialog then the default on the browser gives you.

look at jQueryUI confirm dialog You can style it as you want

this is how you implement it:

  <div id="dialog" title="Confirmation Required">
  Are you sure about this?
  </div>

<script type="text/javascript">
$(document).ready(function() {
$("#dialog").dialog({
  autoOpen: false,
  modal: true
});
 });



$("#dialog").dialog({
  buttons : {
    "Confirm" : function() {
    // do something remove()
    },
    "Cancel" : function() {
      $(this).dialog("close");
    }
  }
});

   $("#dialog").dialog("open");
 });

adardesign
That is fantastic, I will definitely look into that. It is much more appealing than the default and seems to be IE friendly
CarterMan