Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I close a dialog using jQuery?

Tags:

I am using the code below to create a jQuery UI Dialog widget dynamically:

 $(function () {         var Selector = $("a:contains('sometext')");         $(Selector).bind('click', function () {             var NewDialog = "<div dir=rtl id='MenuDialog'></div>";             var DialogContetn = '<div dir=rtl ><table width=100%><tr><td><textarea id="txtRequestContent" cols="30" rows="2"></textarea></td><td><table><tr><td><input id="btnSendEditionRequest" type="button" value="Send" /></td></tr><tr><td><input id="btnCloseDialog" type="button" value="Cancel" /></td></tr></table></td></tr></table></div>';             $('body').append(NewDialog);             $('#MenuDialog').html(DialogContetn);             $('#MenuDialog').hide();             $('#MenuDialog').dialog({ modal: true, title: "title", show: 'clip', hide: 'clip' });             $("#btnCloseDialog").live('click', function () {                 $("#MenuDialog").dialog('close');             });             return false;         });     }); 

First time it loads, the jQuery Dialog works correctly and when I click on the btnCloseDialog the jQuery Dialog closes successfully.

However, after that, the btnCloseDialog no longer closes the dialog. Why is this happening?

Update

I put my code out on a jsfiddle.

This is strange behavior because the button closes the dialog properly in the jsFiddle, but not on the dialog in my project.

like image 690
Shahin Avatar asked Jun 15 '11 07:06

Shahin


People also ask

How do I close dialog?

Right-click the icon referring to the dialog box from the Windows taskbar and click "Close". Again, you might end up with others by doing so, but that dialog box will vanish.

How do I close a dialog box in JavaScript?

Close Dialog when clicking outside of its region in JavaScript Dialog control. By default, dialog can be closed by pressing Esc key and clicking the close icon on the right of dialog header. It can also be closed by clicking outside of the dialog using hide method.

What is dialog in jquery?

A dialog box is a floating window with a title and content area. This window can be moved, resized, and of course, closed using "X" icon by default. jQueryUI provides dialog() method that transforms the HTML code written on the page into HTML code to display a dialog box.


2 Answers

Because this shows up early in the search for creating a dynamic dialog in jquery, I'd like to point out a better method to do this. Instead of adding your dialog div and content to the HTML and then calling it, you can do this much more easily by shoving the HTML directly into a jquery object, as so:

$(function () {     $("a:contains('sometext')").click(function() {         var NewDialog = $('<div id="MenuDialog">\             <p>This is your dialog content, which can be multiline and dynamic.</p>\         </div>');         NewDialog.dialog({             modal: true,             title: "title",             show: 'clip',             hide: 'clip',             buttons: [                 {text: "Submit", click: function() {doSomething()}},                 {text: "Cancel", click: function() {$(this).dialog("close")}}             ]         });         return false;     }); }); 

I've also showed how you can put multiple buttons with inline functions, instead of attaching a live() function to the button. I've used this method in a couple of places and it works great for me. It also supports forms (I grabbed the data in doSomething() and submitted through ajax, but other methods work too), etc.

like image 77
Évelyne Lachance Avatar answered Sep 19 '22 18:09

Évelyne Lachance


You should probably not use ids for dynamically created content, as you could end up with more than one element with the same id - meaning that document.getElementById (which I assume sizzle uses for the #id selector) will only return the first (potentially non-visible) one.

like image 31
kͩeͣmͮpͥ ͩ Avatar answered Sep 18 '22 18:09

kͩeͣmͮpͥ ͩ