How to close jQuery Dialog within the dialog?

JqueryJquery UiJquery Ui-Dialog

Jquery Problem Overview


How to close jQuery Dialog within the dialog without using the close button?

Inside the Dialog is a simple form request. If a successful submission occurs, then the UI dialog automatically closes and refreshes the parent page:

<script type="text/javascript">
	$(document).ready(function () {
		$("#form-dialog").dialog({
			autoOpen: true,
			modal: true,
			width: 200,
			draggable: true,
			resizable: true
		});
	});
</script>

<div id="form-dialog" title="Form Submit">
	<form action="default.aspx" method="post">
		<input type="text" name="name" value=" " />    
		<input type="submit" value="submit" />

		<a href="#" id="btnDone">CLOSE</a>

		<script type="text/javascript">
		$(document).ready(function () {
			$("#btnDone").click(function () {
				$(this).dialog('close');
			});
		});
		</script>
	</form>
</div>

Jquery Solutions


Solution 1 - Jquery

you can close it programmatically by calling

$('#form-dialog').dialog('close')

whenever you want.

Solution 2 - Jquery

Try This

$(this).closest('.ui-dialog-content').dialog('close'); 

It will close the dialog inside it.

Solution 3 - Jquery

You need

$('selector').dialog('close');

Solution 4 - Jquery

Close from iframe inside a dialog:

window.parent.$('.ui-dialog-content:visible').dialog('close');

Solution 5 - Jquery

$(this).parents(".ui-dialog-content").dialog('close')

Simple, I like to make sure I don't:

  1. hardcode dialog #id values.
  2. Close all dialogs.

Solution 6 - Jquery

After checking all of these answers above without luck, the folling code worked for me to solve the problem:

$(".ui-dialog").dialog("close");

Maybe this will be also a good try if you seek for alternatives.

Solution 7 - Jquery

replace one string to

$("#form-dialog").dialog('close');

$(this) here means another object $("#btnDone")

 <script type="text/javascript">
    $(document).ready(function () {
        $("#form-dialog").dialog({
            autoOpen: true,
            modal: true,
            width: 200,
            draggable: true,
            resizable: true
        });
    });
</script>


<div id="form-dialog" title="Form Submit">
<form action="default.aspx" method="post">
<input type="text" name="name" value=" " />    
<input type="submit" value="submit" />

<a href="#" id="btnDone">CLOSE</a>

<script type="text/javascript">
$(document).ready(function () {
    $("#btnDone").click(function () {
 //I've replaced next string
 // $(this) here means another object $("#btnDone")
  $("#form-dialog").dialog('close');
    });
});
</script>

</form>
</div>

Solution 8 - Jquery

Using $(this).dialog('close'); only works inside the click function of a button within the modal. If your button is not within the dialog box, as in this example, specify a selector:

$('#form-dialog').dialog('close');

For more information, see the documentation

Solution 9 - Jquery

better way is "destroy and remove" instead of "close" it will remove dialog's "html" from the DOM

$(this).closest('.ui-dialog-content').dialog('destroy').remove();

Solution 10 - Jquery


$(document).ready(function () {
$("#form-dialog").dialog({
autoOpen: true,
modal: true,
width: 200,
draggable: true,
resizable: true,
buttons: {
"Close": function () {
$("#idDialog").dialog("close");
}
}
});
});

This will make you a button to close. you can also call the function close

$("#idDialog").dialog("close");

in some function to do this. or even in a button/a

< a href="javascript:void(0);" id="btnDone"   
                              onClick="$("#idDialog").dialog("close");">CLOSE</a>

EDIT: you need this to include your dialog into form:

open: function (type, data) {
$(this).parent().appendTo($("form:first"));
}

Solution 11 - Jquery

Adding this link in the open

$(this).parent().appendTo($("form:first"));

works perfectly.

Solution 12 - Jquery

If you're using jconfirm the way of closing it programatically is the following:

jconfirm.instances[0].close();

Solution 13 - Jquery

It helped me:

$(".ui-dialog-titlebar-close").trigger('click');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionimperialxView Question on Stackoverflow
Solution 1 - JqueryJasonView Answer on Stackoverflow
Solution 2 - JqueryKunalView Answer on Stackoverflow
Solution 3 - JqueryredsquareView Answer on Stackoverflow
Solution 4 - JqueryDer_MeisterView Answer on Stackoverflow
Solution 5 - JqueryndrixView Answer on Stackoverflow
Solution 6 - JqueryMarcoView Answer on Stackoverflow
Solution 7 - JqueryBartView Answer on Stackoverflow
Solution 8 - JquerykurdtpageView Answer on Stackoverflow
Solution 9 - JqueryVladimir MiroshnichenkoView Answer on Stackoverflow
Solution 10 - JqueryMichel AyresView Answer on Stackoverflow
Solution 11 - JqueryShanView Answer on Stackoverflow
Solution 12 - JqueryMauro BilottiView Answer on Stackoverflow
Solution 13 - JqueryNikView Answer on Stackoverflow