views:

115

answers:

2

Here's a quick question that's probably simpler than I'm thinking.

I'm populating a jQuery UI dialog with the resulting html from an $.ajax() call. In certain cases that html includes a button that when clicked, I want to close the containing dialog.

Assuming that I don't know anything about the element that was specified to be the dialog using $("#element").dialog()...(i.e. I have no idea what the "#element" selector is from the dialog's inner content) - What's the best way to close the dialog from an element clicked inside of it?

+1  A: 

Something like:

$('a.close').click(function(e) {
    $(this).closest('.dialog').dialog('close');
});
Infinity
what if the element is nested?
nickf
I was fixing my sample just when you wrote that
Infinity
+3  A: 

Use closest()

$(this).closest('.ui-dialog-content').dialog('close');
nickf
Interesting... it doesn't actually appear that .ui-dialog is the right selector. Dialog barfs out a few divs, and the one that seems to actually need dialog('close') called on it isn't ".ui-dialog"... still investigating...
womp
looks like .closest('.ui-dialog-content').dialog('close'); is what is needed.
womp