views:

84

answers:

1

Hi,

I'm trying to show a dialog (div) that's inside another div. This way, I can easily keep all my dialogs together.

The page looks something like this:

<div id="bookshelf">
    <div id="login">dialog</div>
</div>

I've added the needed properties to it:

$("div#bookshelf div#login").dialog({ autoOpen: false });

and try to make it show up:

$("div#bookshelf div#login").dialog("open");

and it won't.

However, if I change the last line to

$("div#login").dialog("open");

It does! But I don't want to refer to it directly, because propably something else on my page will be called "login" as well, at some point. And I wanted to stop making those very long id's like id="lp_dialogs_bookshelf_login".

Am I doing something wrong here? Or should I just forget about it, and start using those nasty id's again?

+8  A: 

When you created the dialog, it moved this:

<div id="login">dialog</div>

To the end of your html document, just before </body>, so the selector $("div#bookshelf div#login") doesn't find it...because it isn't inside there anymore.

I would just use div#login in all cases since it should be unique, but to make your example work, you need to move the dialog after it's created, like this:

$("div#bookshelf div#login").dialog({ autoOpen: false })
                            .parent().appendTo('#bookself');
Nick Craver
Nice explanation +1
Raja
I'll give the dialog a different, longer id, and will reference it directly, but it's great to learn exactly why it didn't work! Thank you very much!
Heras
@Heras - Welcome :)
Nick Craver