views:

161

answers:

2

I would like to show in dialog all elements with specific class. The dialog should hide the rest of the page. So for example:

On this SO page I want to show all elements with class="user-info". Those elements would be shown in dialog with same width and height and same css and everything else would be hidden. It would be like cutting them out of the page and pasting them in a dialog.

Any ideas how this could be done?

+4  A: 

I would like to show in dialog all elements with specific class.

So clone those elements, e.g.:

var $div = $("<div />").append($(".fooClass").clone()).dialog();

The dialog should hide the rest of the page.

Either set the overlay graphic (which you can do using themeroller) to something opaque, or attach some code to the open and close events:

$div.dialog({
   open: function(event, ui) { $("body").hide() } // that will hide everything, including the dialog, so watch out.
   close: function(event, ui) { $("body").show() }
});

Proof of concept here.

EDIT: This demo keeps the inline style defined in a parent element.

karim79
+1 Hey that's clever. Nicely done!
Chuck Conway
yes, clone was my first though, but the problem is that Clone() doesn't copy parent's style. Example:http://jsfiddle.net/rvbmh/
grega g
@grega g: Why are you using in-line styles?
fudgey
no reason, its the same if I move style to css 'file': http://jsfiddle.net/Nda55/
grega g
Yeah, but the CSS in a file or defined inside of style tags would apply to the dialog content. @karim79: Nice demo, but you could add some UI styling hosted by google to make the dialog window more visible ;) http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css
fudgey
@fudgey - thanks for that! done: http://jsfiddle.net/Nda55/2/
karim79
@grega g - done, but it's not all that pretty: http://jsfiddle.net/Nda55/6/
karim79
This takes care of first level parent, but i would need it for arbitrary DOM. How about getting 'calculated' values of width, height, font-size,... from original element and setting them on clone. And doing this recursively for all sub-elements. Or maybe jquery.extend() function would do the trick.
grega g
A: 

Found an answer thanks to this post

Check it out here. It demonstrates pulling all elements of certain class from iframe and than appending them to main document and copying their style. Problem is that its very slow, especially if we copy many elements with a lot of child elements. If anyone knows a way to improve performance let me know (post here:)).

note: The reason I loaded jsFiddle page in iframe is that it(browser?) won't let jquery inspect content of iframe that's not loaded from same domain.

grega g