2

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

3
  • Have you tried creating your div tags using jQuery from the very beginning? var div_day = $("<div>").html("Tuesday"); I'm not entirely sure what your problem is without seeing more code, but it's a thought... Commented Jul 25, 2011 at 16:36
  • If you c/p'd as is, you're missing a semicolon after the first line Commented Jul 25, 2011 at 16:37
  • @tkm256 Semicolon would not cause this problem because of the carriage return after the line. Commented Jul 25, 2011 at 16:39

3 Answers 3

2
div_day.appendChild(div_dateValue)
$('#calendar').append(div_day)  
Sign up to request clarification or add additional context in comments.

Comments

2

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>");

2 Comments

so if I was setting a bunch of div settings, I would just do it in line? such as: div_day = $("<div id='blah' style='etc etc etc'>");
div_day = $( "<div>" ).css( 'color', 'red' ).css( 'width', '10px' ).etc
1
$('<div><div></div></div>').appendTo("#calendar");

Try this and mark it as answer if it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.