0

I have a button in my HTML, once click on the button, I want to open a JQuery Dialog. I have a version of code which works fine, but I just want to reorganize the code, seems like there's something wrong with it since the dialog cannot be displayed anymore.

the version that works:

<script type="text/javascript">
$(function() {
   $('#dialog_trigger').on("click", function() {
       $('#dialog').load('index.php', function() {
          $('#dialog').dialog({
           *********(somehow I must remove 'autoOpen: false' here, otherwise it also stops working) ********

              position: 'center',
              width : 480,
              height : 320, 
              modal : true
          });

        });
      });
  });

</script>

<body>
<button id="dialog_trigger">Click me</button>
<div id="dialog"></div>
</body>

the code that doesn't work:

<script type="text/javascript">
$(function() {
   $('#dialog_trigger').on("click", function() {
       $('#dialog').load('index.php', function() {
          $('#dialog').dialog("open")
       });
    });

   $('#dialog').dialog({
    autoOpen: false,
    position: 'center',
    width : 480,
    height : 320, 
    modal : true
   });

});

</script>

<body>
<button id="dialog_trigger">Click me</button>
<div id="dialog"></div>
</body>

Please help me correct it, thanks.

2
  • $('#dialog').dialog("") I don't think that's right, what are you really trying to do there? Commented Jul 18, 2013 at 0:33
  • opps, sorry, copy error, originally is $('#dialog').dialog("open"). fixed it. Commented Jul 18, 2013 at 0:39

1 Answer 1

2

Firstly move the signature for the dialog to outside the click event.

Then in the click event you can use

$('#dialog').dialog("open")

to show the dialog

Check Fiddle

Code

  $('#dialog_trigger').on("click", function() {
       $('#dialog').load('index.php').dialog("open")
    });

   $('#dialog').dialog({
     autoOpen: false,
     position: 'center',
     width : 480,
     height : 320, 
     modal : true
   });
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry, there's a copy error before, I do call the "$('#dialog').dialog("open")" inside the click event, and define the dialog outside. However, it doesn't work, I mean, after I clicking the button, nothing happens.
and Sushanth, I want to load a web page into the dialog, please see my first version of code, can you help fix the second one according to what I want in the first one?
@user1453951.. Why don't you try this $('#dialog').load('index.php').dialog("open")
huh, that's what I want, thanks!!! Btw, do you know what's the difference if I put "open" to the dialog() or not? Is it a function parameter?
open is a method available on the dialog object .. If you just call .dialog() instead of specifying a method , it will just create a new instance of dialog box which is not what you want
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.