Simple jQueryUI Dialog causes error with 1.9
Asked Answered
M

1

0

I've previously used the following script and it worked perfectly. I am now getting the following error:

Error: cannot call methods on dialog prior to initialization; attempted to call method 'close'.
jquery-latest.js (line 507)

If you upgrade to jQueryUI 1.10.1, it no longer causes an error. Normally, I would just let it go, upgrade, and not worry about it. Problem with this one is that it worked in the past so why not now is driving me crazy? I must be doing some silly thing different than previously, but I cannot see it.

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>jQuery UI Dialog</title>
        <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script> 
        <link type="text/css" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" rel="stylesheet" />
        <script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js" type="text/javascript"></script> 
        <script>
            $(function() {
                $("#open").click(function(){$("#dialog").dialog('open');return false;});
                $( "#dialog" ).dialog({
                    modal: true,
                    autoOpen    : false,
                    buttons: {
                        Ok: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            });
        </script>
    </head>
    <body>
        <button id="open">Click</button>
        <div id="dialog" title="What ever"></div>
    </body>
</html>
Modifier answered 3/3, 2013 at 16:52 Comment(6)
If it works with no problems in a newer version it must be a bug with the version you are using. I know it drives you crazy but that’s the life of a web developer. Stuff is constantly changing, sometimes even hour by hour!Brosy
Turns out it worked perfectly for me using jQuery 1.8.1, not using the latest version of jQuery!Modifier
That doesn’t matter. Bugs get introduced all the time and then fixed again in a later version, but your question mentioned that it worked correctly in jQuery 1.10.1 so I thought it was resolved by a bug fix. There are many known issues in 1.9, that’s why 1.9.1 was released so shortly afterwards.Brosy
At least I am no longer going crazy! I now know it didn't previously work under identical circumstances, and then mysteriously stop working.Modifier
Have you tried using the jQuery migrate plugin that they recommend using when upgrading to 1.9?Zygosis
@j08691. No. Should I?Modifier
S
0

I had a similar problem that i resolved by defining my button array outside of the dialog declaration.

var buttonArray = {};
buttonArray["Ok"]=function() { $( this ).dialog( "close" );}

Your options would become:

     modal: true,
     autoOpen: false,
     buttons: buttonArray
Seamus answered 5/11, 2013 at 20:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.