views:

10556

answers:

8

I am try to use JQuery UI Dialog to replace the ugly javascript:alert() box. In my scenario, I have a list of items, and next to each individual of them, I would have a "delete" button for each of them. the psuedo html setup will be something follows:

<ul>
        <li>ITEM <a href="url/to/remove"> <span>$itemId</span>
        <li>ITEM <a href="url/to/remove"><span>$itemId</span>
        <li>ITEM <a href="url/to/remove"><span>$itemId</span>
    </ul>
    <div id="confirmDialog">Are you sure?</div>

In JQ part, on document ready, I would first setup the div to be a modal dialog with necessary button, and set those "a" to be firing to confirmation before to remove, like:

$("ul li a").click(function()
{
  // Show the dialog

  return false; // to prevent the browser actually following the links!
}

OK, here's the problem. during the init time, the dialog will have no idea who (item) will fire it up, and also the item id (!). How can I setup the behavior of those confirmation buttons in order to, if the user still choose YES, it will follow the link to remove it?

+1  A: 

Will this do?

$("ul li a").click(function(e) {
  e.preventDefault();
  $('#confirmDialog').show();

  var delete_path = $(this).attr('href');

  $('#confirmDialog a.ok').unbind('click'); //  just in case the cancel link 
                                            //  is not the  only way you can
                                            //  close your dialog
  $('#confirmDialog a.ok').click(function(e) {
     e.preventDefault();
     window.location.href = delete_path;
  });

});

$('#confirmDialog a.cancel').click(function(e) {
   e.preventDefault();
   $('#confirmDialog').hide();
   $('#confirmDialog a.ok').unbind('click');
});
andi
Thanks for this reply. I am sure I will test it out (and yet it looks functional). One of the problem I saw from many answers here is the lack of generality. If the page has another set of controls (or links or so on) that needed confirmation, seems we need multiple declaration on the interaction / action.The old javascript way, ie, href="javascript:confirm('link_url');" is simple and elegant and suits all similar cases. Of course the javascript method is too obstrusive that people w/o javascript will entirely miss the link. Once again thx for the great reply.
xandy
+6  A: 

why reinventing the wheel?

here is are some jQuery alert boxes and dialog boxes

Natrium
Nice link - thanks!
Aaron
Very nice library - however you aren't answering how you exchange information between the main code flow and the confirmation dialog's callback.
kgiannakakis
+1, excellent stuff!
ammoQ
+1  A: 

How about this:

$("ul li a").click(function() {

el = $(this);
$("#confirmDialog").dialog({ autoOpen: false, resizable:false,
                             draggable:true,
                             modal: true,
                             buttons: { "Ok": function() {
                                el.parent().remove();
                                $(this).dialog("close"); } }
                           });
$("#confirmDialog").dialog("open");

return false;
});

I have tested it at this html:

<ul>
<li><a href="#">Hi 1</a></li>
<li><a href="#">Hi 2</a></li>
<li><a href="#">Hi 3</a></li>
<li><a href="#">Hi 4</a></li>
</ul>

It removes the whole li element, you can adapt it at your needs.

kgiannakakis
+11  A: 

I just had to solve the same problem. The key to getting this to work was that the dialog must be partially initialized in the click event handler for the link you want to use the confirmation functionality with (if you want to use this for more than one link). This is because the target URL for the link must be injected into the event handler for the confirmation button click. I used a CSS class to indicate which links should have the confirmation behavior.

Here's my solution, abstracted away to be suitable for an example.

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

  $(".confirmLink").click(function(e) {
    e.preventDefault();
    var targetUrl = $(this).attr("href");

    $("#dialog").dialog({
      buttons : {
        "Confirm" : function() {
          window.location.href = targetUrl;
        },
        "Cancel" : function() {
          $(this).dialog("close");
        }
      }
    });

    $("#dialog").dialog("open");
  });
</script>

<a class="confirmLink" href="http://someLinkWhichRequiresConfirmation.com"&gt;Click here</a>
<a class="confirmLink" href="http://anotherSensitiveLink"&gt;Or, you could click here</a>

I believe that this would work for you, if you can generate your links with the CSS class (confirmLink, in my example).

In the interest of full disclosure, I'll note that I spent a few minutes on this particular problem and I provided a similar answer to this question, which was also without an accepted answer at the time.

Paul Morie
+1 almost works... but see answer from @lloydphillips for a correction
rohancragg
+5  A: 

I found the answer by Paul didn't quite work as the way he was setting the options AFTER the dialog was instantiated on the click event were incorrect. Here is my code which was working. I've not tailored it to match Paul's example but it's only a cat's whisker's difference in terms of some elements are named differently. You should be able to work it out. The correction is in the setter of the dialog option for the buttons on the click event.

$(document).ready(function(){

    $("#dialog").dialog({
      modal: true,
            bgiframe: true,
            width: 500,
            height: 200,
      autoOpen: false
      });


    $(".lb").click(function(e) {
        e.preventDefault();
        var theHREF = $(this).attr("href");


        $("#dialog").dialog('option', 'buttons', {
                "Confirm" : function() {
                    window.location.href = theHREF;
                    },
                "Cancel" : function() {
                    $(this).dialog("close");
                    }
                });

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

    });

});

Hope this helps someone else as this post originally got me down the right track I thought I'd better post the correction.

lloydphillips
+1  A: 

As above. Previous posts got me on the right track. This is how I've done it. The idea is to have an image next to every row in the table (generated by php script from database). And image when clicked would redirect to url that would delete the appropriate record in the database while showing some data related to clicked record within jQuery Dialog.

Javascript:

$(document).ready(function(){
  $("#confirmDelete").dialog({
                              modal: true,
                              bgiframe: true,
                              autoOpen: false }); 
});

function confirmDelete(username, id) {
  var delUrl = "/users/delete/" + id;
  $('#confirmDelete').html("Are you sure you want to delete user: '" + username + "'");
  $('#confirmDelete').dialog('option', 'buttons', { 
    "No": function() { $(this).dialog("close"); },
    "Yes": function() { window.location.href = delUrl; }  });
  $('#confirmDelete').dialog('open');
}

Dialog div:

<div id="confirmDelete" title="Delete User?"></div> 

Image link:

<img src="img/delete.png" alt="Delete User" onclick="confirmDelete('<?=$username;?>','<?=$id;?>');"/>

This way you can pass over the php loop values into the dialog box. The only downside is using 'get' to actually perform the action.

LukeP
A: 

in the above answer when you use window.location it will not pass http_referer to server

Anil
A: 

It may be too simple for your needs, but you could try this jQuery confirm plugin

It's really simple to use and does the job for many cases.

grahamesd