Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a jquery ui dialog object?

Say I have a dialog open that has no "id" how can I find the dialog and get the dialog object so I can do .dialog('close') on it?

Example

// say if this was my dialog
<div> 
   <input type="button" id="btn" />
</div>

$("#btn").parents("div").dialog('close');

This does not work though so I need to get the actual object.

like image 598
chobo2 Avatar asked Apr 21 '11 21:04

chobo2


People also ask

What is jQuery UI dialog?

The jQuery UI dialog method is used to create a basic dialog window which is positioned into the viewport and protected from page content. It has a title bar and a content area, and can be moved, resized and closed with the 'x' icon by default.

How do you check if jQuery dialog is initialized?

A more reliable solution would be to add your own existence indicator at dialog initialization: $("#popup"). attr("_dialogInitialized", "yes"). dialog( { ... } )

How make jQuery UI dialog responsive?

Below is how I achieved a responsive jQuery UI Dialog. To do this, I added a new option to the config - fluid: true , which says to make the dialog responsive. I then catch the resize and dialog open events, to change the max-width of the dialog on the fly, and reposition the dialog.


2 Answers

That's the very reason you should have an id on those divs. Consider the following options:

  1. Consider adding the id to the markup. That is easy to do and maintain.

  2. Otherwise, when you get the div(s) originally, before performing a .dialog(), give them dynamic id's: el.attr('id', 'dialogBox').

  3. If you don't want to give them id's (for some strange reason), you still have them at some point in time in your js code, so save the references to those objects. Later on, refer to the required reference and you can call .dialog('close'). That will also perform caching for you, so you don't need to search the DOM tree again.

  4. As a last resort, if you don't want to do the above, then refer to them the same way you did originally (this isn't always a good idea, especially if the DOM changes).

Although just for reference, your example (which employs the last option) works: http://jsfiddle.net/vbcMW/

like image 115
davin Avatar answered Oct 23 '22 01:10

davin


I believe finding the closest div with class ui-dialog-content should work:

$("#btn").click(function() {
    $(this).parents("div.ui-dialog-content").dialog("close");
});

(.ui-dialog-content is applied to the original div, which is then wrapped in a few other divs)

Here's a working example: http://jsfiddle.net/HPkvZ/

like image 4
Andrew Whitaker Avatar answered Oct 23 '22 02:10

Andrew Whitaker