Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create jQuery Dialog in function

Tags:

jquery-ui

Does anyone know how to create a jQuery Dialog in a function? I can't find an attribute to set the message... In every example I found, the dialog has been statically written into the code in a div-tag. However, I want to create it dinamically, so I need to know how to create a dialog in a function.

It is no problem to set the title:

    <script>
    // increase the default animation speed to exaggerate the effect
    $.fx.speeds._default = 1000;
    $(function() {
        $( "#dialog" ).dialog({
            autoOpen: false,
            show: "blind",
            hide: "explode"
        });

        $( "#opener" ).click(function() {
            //$( "#dialog" ).dialog( "open" );
            $( this ).dialog({ title: 'Please confirm deletion!' });
            return false;
        });
    });
    </script>
</head>
<body>

I have the documentation and some examples here.

Thanks for helping out guys.

Cheers, doonot

============================= [SOLUTION]=====================================

Thanks for all who answered this questions. This is how i wanted it:

    function createDialog(title, text) {
    return $("<div class='dialog' title='" + title + "'><p>" + text + "</p></div>")
    .dialog({
        resizable: false,
        height:140,
        modal: true,
        buttons: {
            "Confirm": function() {
                $( this ).dialog( "close" );
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        }
    });
}

And it can be called for example like this (clicking on an image):

<img src="delete.png" onClick="createDialog('Confirm deletion!', 'Do you really want to delete this package?')">
like image 323
nimrod Avatar asked Feb 02 '11 20:02

nimrod


1 Answers

function createDialog(title, text, options) {
    return $("<div class='dialog' title='" + title + "'><p>" + text + "</p></div>")
    .dialog(options);
}
like image 80
Luke Avatar answered Nov 03 '22 07:11

Luke