views:

35

answers:

4

I want to select recently created element doing something like this:

$("body").append("<div id='popup.dialog'></div>");
dialogDiv = $("#popup.dialog");

But after execution of this dialogDiv contains nothing. So is there're way to select newly createn element?

A: 

I think your problem is the . in the id.

The reason is, that jQuery used the . to match classes, so you are looking for an element with id = popup and class = dialog

googletorp
A: 

You can't have a . in an id.

Try this:

$("body").append("<div id='popup-dialog'></div>");
dialogDiv = $("#popup-dialog");
Tom
+1  A: 

Two things:

1) Dont use periods in your ids

2) you could do it like this a bit better:

var dialogDiv =$('<div id="popup-dialog"></div>').appendTo('body');

At this point you could chain more, or just use the dialogDiv variable.

This stops you from taking a performance hit by selecting an element that you already have access to.

Alex Sexton
A: 

The dot is not valid in the ID. #popup.dialog searches for <div id='popup' class='dialog'>. You should replace it with a dash, like

$("body").append("<div id='popup-dialog'></div>");
dialogDiv = $("#popup-dialog");
Alexander Gyoshev