Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you launch a jquery dialog when you click on a <a href> link

Tags:

jquery

dialog

how do you launch a jquery dialog when you click on a link

this doesn't seem to work

 <script type="text/javascript">
  $(document).ready(function() {

  $("a.To").click(function(e) {
      e.preventDefault();
      $("#dialog").dialog({height:300});
  });

in the body:

<a href="#" id="To">To</a>
like image 900
leora Avatar asked Aug 25 '09 14:08

leora


2 Answers

For id you should use # :

$("a#To")

Dot is for classes.

like image 82
Daniel Moura Avatar answered Oct 05 '22 02:10

Daniel Moura


I did this recently for confirming delete links in my cms. First you should instantiate a dialog window (this is so if you click close on the dialog and then open it again, it shows up...otherwise, it's destroyed):

$(document).ready(function()
{
    /**
     * Create a dialog box for confirming deletes.
     * This creates the box at domready. The box is opened
     * by a call to dialog("open") in the delete link.
     */
    $("#delete-dialog").dialog({
        autoOpen   : false,
        bgiframe   : true,
        buttons    : {
            "Yes, I'm sure" : function()
            {
                $(this).dialog("close");
                var href = $(this).dialog("option", "href");
                var row = $(this).dialog("option", "row");
                $.doDelete(href, row);
            },
            "Cancel" : function()
            {
                $(this).dialog("close");
            }
        },
        height     : 150,
        modal      : true,
        overlay    : {
            backgroundColor : "#000000",
            opacity         : 0.75
        },
        resizable : false
    });
});

Then "hook up" the a tags (still in the document.ready block):

/**
 * Make all delete links confirm before sending to delete path.
 */
$("a.delete-href").live("click", function(event) 
{
    event.preventDefault();

    var href = $(this).attr("href");
    var row = $(this).parent().parent();

    // pass some custom options to the dialog
    $("#delete-dialog").dialog("option", "href", href);
    $("#delete-dialog").dialog("option", "row", row);
    // open the previously init'ed dialog
    $("#delete-dialog").dialog("open");
});
like image 22
typeoneerror Avatar answered Oct 05 '22 00:10

typeoneerror