Bring Coffee Bring Beer
Bring Coffee Bring Beer

Reputation: 1121

Adding a dynamically created div to another div with jquery

I am creating two divs:

var div_day=document.createElement("div");
var div_dateValue=document.createElement("div");

I then want to add div_day to an existing calendar div and div_dateValue to div_day:

$('#calendar').append(div_day)                          
$(div_day).append(div_dateValue);

div_day gets added to calendar, but div_dateValue does not get added to div_day, and the script stops there. No errors in the console but it is in a loop and should have more div_days (each with a unique id). I am new to jquery so any help is appreciated.

In my search I have found how to add divs, but not to add a dynamically created div to another dynamically created div.

Thanks for your help!

Kevin

Upvotes: 2

Views: 19511

Answers (3)

Jeff B
Jeff B

Reputation: 30099

Something else must be going on (even with your missing semi-colon). Your example works fine here:

http://jsfiddle.net/P4rh5/

But, instead of creating divs with straight javascript, you can do it with jQuery:

var div_day = $("<div>");
var div_dateValue = $("<div>");

$('#calendar').append(div_day);                        
$(div_day).append(div_dateValue);

Of course, you could do this in a single step:

$('#calendar').append("<div><div></div></div>");

Upvotes: 2

Najam Awan
Najam Awan

Reputation: 1145

$('<div><div></div></div>').appendTo("#calendar");

Try this and mark it as answer if it helps

Upvotes: 1

Joseph Marikle
Joseph Marikle

Reputation: 78520

div_day.appendChild(div_dateValue)
$('#calendar').append(div_day)  

Upvotes: 2

Related Questions