Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery dialog button how to set the click event?

Ok i got this code:

    $(document).ready(
    function() {
        $(".dialogDiv").dialog({
            autoOpen: false,
            modal: true,
            position: [50, 50],
            buttons: {
                "Print page": function() {
                    alert("Print");
                },
                "Cancel": function() {
                    $(this).dialog("close");
                }
            }
        }
        );
    $('.ui-dialog-buttonpane button:contains("Print page")').attr("id", "dialog_print-button");
    $(".dialogDiv").parent().appendTo($('form'));
    }

How do I assign or set a new function to the click event?

$("#dialog_print-button"). ???

Edit, This works:

$("#dialog_print-button").unbind("click").click(
function () {
   alert("new function that overide the old ones")
}
)

Tried to find how to do in the jQuery documentation but I think it's hard to find around in the documentation. Especially when new to javaScript and the jQuery libary.

Edit, A fast way to get help is to go to jQuery irc channel :D

like image 635
The Demz Avatar asked Aug 05 '11 11:08

The Demz


2 Answers

I think this would help:

$(".dialogDiv").dialog("option", "buttons", {
    "Print page": function() { /* new action */ },
    "Cancel": function() { $(this).dialog("close"); }
});

Because buttons property sets all the buttons, you have to include cancel button handler.

like image 121
Igor Dymov Avatar answered Sep 20 '22 04:09

Igor Dymov


$("#Print page").click(function () {
   ...
});

Or maybe it should be

$("#dialog_print-button").click(function () {
   ...
});
like image 6
Mati Avatar answered Sep 19 '22 04:09

Mati