jQueryUI Dialog Does Not Close

Karl Kieninger

I think I'm doing this right, but neither button actually closes. Either I'm blind or missing a subtlety.

jsFiddle

HTML

<div id="sample-dialog" title="New Effect"></div>
<button>Button</button>

JS

$(document).ready(function () {
    $(function () {

        $(function () {
            $("button")
                .button()
                .click(function (event) {
                event.preventDefault();
                $("#sample-dialog").dialog("open");
            });
        });

        $("#sample-dialog").dialog({
            autoOpen: false,
            height: 200,
            width: 200,
            modal: true,
            buttons: {
                "Close by $(#id)": function () {
                    $("#sample-dialog").dialog("close");
                },
                    "Close by $(this)": function () {
                    $(this).dialog("close");
                }
            }
        });

    });

});

I started the process trying to follow and modify this sample.

Jason P

Apparently wrapping the button click handler in $(function() {...}) causes that to be executed after the function to create the dialog. The buttons in the dialog are, well, <button>s, so clicking them closes the dialog, but the click handler immediately reopens the dialog. It works without the extra $(function() {...}), which shouldn't be necessary anyway:

http://jsfiddle.net/nLvwp/

$(document).ready(function () {
    $("button")
        .button()
        .click(function (event) {
        event.preventDefault();
        $("#sample-dialog").dialog("open");
    });

    $("#sample-dialog").dialog({
        autoOpen: false,
        height: 200,
        width: 200,
        modal: true,
        buttons: {
            "Close by $(#id)": function () {
                $("#sample-dialog").dialog("close");
            },
                "Close by $(this)": function () {
                $(this).dialog("close");
            }
        }
    });
});

You could also solve the problem by being more specific in selecting the button that should open the dialog. But a single $(document).ready(function{...}) or $(function() {...}) (which do the same thing) is sufficient.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQueryUI dialog scrolls to top when clicking close button in IE

From Dev

jQueryUI dialog scrolls to top when clicking close button in IE

From Dev

How do I get rid of this blue halo around the close dialog button with jqueryui?

From Dev

How to move the jQueryUI close button on the dialog over the top of top right corner?

From Dev

jQueryUI dialog replacement for confirm?

From Dev

jQueryUI dialog replacement for confirm?

From Dev

Activity does not close when calling finish() in alert dialog

From Dev

JqueryUI and bootstrap interference with dialog display

From Dev

Knockoutjs Custom bindig with jQueryUI Dialog

From Dev

JqueryUI - Dialog open multiple times

From Dev

Disable dialog close for ProgressMonitor

From Dev

custom dialog with close button

From Dev

How to close a dialog in code?

From Dev

How to close the Angular dialog?

From Dev

Close dialog window on webpage

From Dev

How to close a dialog in code?

From Dev

Javascript dialog window close

From Dev

Jquery Model dialog close

From Dev

Jquery UI dialog and Close

From Dev

Selenium: close dialog box

From Dev

alert dialog unable to close

From Dev

MSAL browser dialog close

From Dev

JQuery dialog close

From Dev

"Something" is breaking this jqueryui dialog. What is it?

From Dev

jQueryUI load page into DIV and into modal dialog

From Dev

Attach events to child controls in JQueryUI dialog

From Dev

loading jqueryUI dialog() function on WordPress page

From Dev

How to position a JQueryUI datepicker dialog via a MouseEvent

From Dev

Access jQueryUI Dialog buttons upon creation