Just save the reference that jQuery returns from the .dialog
invocation:
var magic = $('<div>').html("Ta-da!").dialog();
You can then use that reference later to open the popup again:
$(magic).dialog('open');
or to delete it entirely (along with its generated .parent
wrapper):
$(magic).parent().remove();
You can even have it delete itself when it is closed, by creating it with the close
option (or appending it later):
close: function(ev, ui) { $(this).remove(); }
var magic = null;
function createMagic() {
magic = $('<div>').html("Ta-da!").dialog({
modal: true,
title: 'Not from the DOM',
buttons:[{
click: function () { $(this).dialog("close"); },
text: 'Close Me'
}],
show: false,
//close: function(ev, ui) { $(this).remove(); }
});
updateMagicStatus();
}
function showMagic() {
$(magic).dialog('open');
updateMagicStatus();
}
function killMagic() {
$(magic).parent().remove();
updateMagicStatus();
}
function updateMagicStatus() {
$('#MagicStatus').text(magic);
$('#MagicPopStatus').text($('div.ui-dialog').length);
}
$(document).ready(updateMagicStatus);
<link href="https://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.2/jquery-ui.min.js"></script>
<div style='cursor:pointer'>
<a onclick="createMagic();">Make a Magic Modal</a>
<br/><br/>
<a onclick="showMagic();">Show the Magic Modal</a>
<br/><br/>
<a onclick="killMagic();">Kill the Magic Modal</a>
</div>
<br/>
Magic object is currently: <label id="MagicStatus" style='color:red'></label>
<br/>
jQUery UI popup wrappers: <label id="MagicPopStatus" style='color:red'></label>