jQuery UI Dialog Box - does not open after being closed
Asked Answered
J

12

79

I have a problem with the jquery-ui dialog box.

The problem is that when I close the dialog box and then I click on the link that triggers it, it does not pop-up again unless I refresh the page.

How can I call the dialog box back without refreshing the actual page.

Below is my code:

$(document).ready(function() {
    $('#showTerms').click(function()
    {
        $('#terms').css('display','inline');
        $('#terms').dialog({
            resizable: false,
            modal: true,
            width: 400,
            height: 450,
            overlay: { backgroundColor: "#000", opacity: 0.5 },
            buttons:{ "Close": function() { $(this).dialog("close"); } },
            close: function(ev, ui) { $(this).remove(); },
    }); 
});

Thanks

Janettjanetta answered 14/12, 2008 at 19:9 Comment(0)
J
14

I solved it.

I used destroy instead close function (it doesn't make any sense), but it worked.

$(document).ready(function() {
$('#showTerms').click(function()
{
    $('#terms').css('display','inline');
    $('#terms').dialog({resizable: false,
        modal: true,
        width: 400,
        height: 450,
        overlay: { backgroundColor: "#000", opacity: 0.5 },
        buttons:{ "Close": function() { $(this).dialog('**destroy**'); } },
        close: function(ev, ui) { $(this).close(); },
    });         
});   
$('#form1 input#calendarTEST').datepicker({ dateFormat: 'MM d, yy' });
});
Janettjanetta answered 14/12, 2008 at 19:42 Comment(2)
Destroy will work if you use that method, but to make close() work, instantiate the dialog first, then use dialog.show() to show it, then dialog.close() to close it, and it will reopen without a problem.Trawick
Almost. You're right about initilizing it first, but after that it's .dialog("open") and .dialog("close")Complication
B
110

You're actually supposed to use $("#terms").dialog({ autoOpen: false }); to initialize it. Then you can use $('#terms').dialog('open'); to open the dialog, and $('#terms').dialog('close'); to close it.

Berzelius answered 13/4, 2009 at 22:43 Comment(3)
This works perfectly. The jQuery UI documents aren't very clear here. But the idea that dialog function is for initialization, showing, and hiding made sense of this for me. Thanks.Incubate
If you're loading this dialog from HTML that can dynamically change, it's very unintuitive why it doesn't work. Anybody have any good solutions that can be applied generically to these situations?Gambetta
@Gambetta You can always use the $(this).remove(); function at the end of each of your dialog's buttons, this way the whole dialog will be completely remade from scratch when you call it again. Note that this function acts differently than $(this).dialog("destroy"); since it only sets the dialog back to its pre-init state, without destroying the object.Detection
J
14

I solved it.

I used destroy instead close function (it doesn't make any sense), but it worked.

$(document).ready(function() {
$('#showTerms').click(function()
{
    $('#terms').css('display','inline');
    $('#terms').dialog({resizable: false,
        modal: true,
        width: 400,
        height: 450,
        overlay: { backgroundColor: "#000", opacity: 0.5 },
        buttons:{ "Close": function() { $(this).dialog('**destroy**'); } },
        close: function(ev, ui) { $(this).close(); },
    });         
});   
$('#form1 input#calendarTEST').datepicker({ dateFormat: 'MM d, yy' });
});
Janettjanetta answered 14/12, 2008 at 19:42 Comment(2)
Destroy will work if you use that method, but to make close() work, instantiate the dialog first, then use dialog.show() to show it, then dialog.close() to close it, and it will reopen without a problem.Trawick
Almost. You're right about initilizing it first, but after that it's .dialog("open") and .dialog("close")Complication
S
12

on the last line, don't use $(this).remove() use $(this).hide() instead.

EDIT: To clarify,on the close click event you're removing the #terms div from the DOM which is why its not coming back. You just need to hide it instead.

Shenyang answered 14/12, 2008 at 19:37 Comment(0)
P
9

I believe you can only initialize the dialog one time. The example above is trying to initialize the dialog every time #terms is clicked. This will cause problems. Instead, the initialization should occur outside of the click event. Your example should probably look something like this:

$(document).ready(function() {
    // dialog init
    $('#terms').dialog({
        autoOpen: false,
        resizable: false,
        modal: true,
        width: 400,
        height: 450,
        overlay: { backgroundColor: "#000", opacity: 0.5 },
        buttons: { "Close": function() { $(this).dialog('close'); } },
        close: function(ev, ui) { $(this).close(); }
    });
    // click event
    $('#showTerms').click(function(){
        $('#terms').dialog('open').css('display','inline');      
    });
    // date picker
    $('#form1 input#calendarTEST').datepicker({ dateFormat: 'MM d, yy' });
});

I'm thinking that once you clear that up, it should fix the 'open from link' issue you described.

Peregrinate answered 27/10, 2009 at 23:40 Comment(0)
B
3

For me this approach works:

The dialog may be closed by clicking the X on the dialog or by clicking 'Bewaren'. I'm adding an (arbitrary) id because I need to be sure every bit of html added to the dom is removed afterwards.

$('<div id="dossier_edit_form_tmp_id">').html(data.form)
.data('dossier_id',dossier_id)
.dialog({
        title: 'Opdracht wijzigen',
        show: 'clip',
        hide: 'clip',
        minWidth: 520,
        width: 520,
        modal: true,
        buttons: { 'Bewaren': dossier_edit_form_opslaan },
        close: function(event, ui){ 
                                  $(this).dialog('destroy'); 
                                  $('#dossier_edit_form_tmp_id').remove();
               }
});
Bistre answered 25/5, 2010 at 9:48 Comment(0)
F
3
 <button onClick="abrirOpen()">Open Dialog</button>

<script type="text/javascript">
var $dialogo = $("<div></div>").html("Aqui tu contenido(here your content)").dialog({
       title: "Dialogo de UI",
       autoOpen: false,
       close: function(ev, ui){
               $(this).dialog("destroy");
       }
 function abrirOpen(){
       $dialogo.dialog("open");
 }
});

//**Esto funciona para mi... (this works for me)**
</script>
Fisherman answered 7/12, 2012 at 11:22 Comment(0)
H
3

This is a super old thread but since the answer even says "It doesn't make any sense", I thought I'd add the answer...

The original post used $(this).remove(); in the close handler, this would actually remove the dialog div from the DOM. Attempting to initialize a dialog again wouldn't work because the div was removed.

Using $(this).dialog('destroy') is calling the method destroy defined in the dialog object which does not remove it from the DOM.

From the documentation:

destroy()

Removes the dialog functionality completely. This will return the element back to its >>pre-init state. This method does not accept any arguments.

That said, only destroy or remove on close if you have a good reason to.

Hel answered 23/10, 2013 at 16:52 Comment(0)
S
2
$(this).dialog('destroy');

works!

Separatist answered 13/4, 2009 at 22:29 Comment(0)
K
1

.close() is mor general and can be used in reference to more objects. .dialog('close') can only be used with dialogs

Kingston answered 28/1, 2010 at 14:50 Comment(0)
O
1

I use the dialog as an dialog file browser and uploader then I rewrite the code like this

var dialog1 = $("#dialog").dialog({ 
              autoOpen: false, 
              height: 480, 
              width: 640 
}); 
$('#tikla').click(function() {  
    dialog1.load('./browser.php').dialog('open');
});   

everything seems to work great.

Optional answered 27/9, 2010 at 13:42 Comment(0)
M
1

I had the same problem with jquery-ui overlay dialog box - it would work only once and then stop unless i reload the page. I found the answer in one of their examples -
Multiple overlays on a same page
flowplayer_tools_multiple_open_close
- who would have though, right?? :-) -

the important setting appeared to be

oneInstance: false

so, now i have it like this -

$(document).ready(function() {

 var overlays = null;

 overlays = jQuery("a[rel]");

 for (var n = 0; n < overlays.length; n++) {

    $(overlays[n]).overlay({
        oneInstance: false, 
        mask: '#669966',
        effect: 'apple',
        onBeforeLoad: function() {
            overlay_before_load(this);
        }
    });

  }

}

and everything works just fine

hope this helps somebody

O.

Mcilwain answered 6/6, 2011 at 16:25 Comment(0)
C
0

The jQuery documentation has a link to this article 'Basic usage of the jQuery UI dialog' that explains this situation and how to resolve it.

Carryingon answered 12/4, 2010 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.