By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,654 Members | 1,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,654 IT Pros & Developers. It's quick & easy.

problems with appendChild and setAttribute properties.

P: 9
Expand|Select|Wrap|Line Numbers
  1.  
  2. for(var i = 0; i < 12; i++)
  3. {
  4.  
  5.     dv = document.createElement('div');
  6.     dv.setAttribute("id",i.toString());      
  7.     dv.className="time";                
  8.     if(i == 0)
  9.     {
  10.         document.getElementById('days').appendChild(dv);
  11.     }
  12.     else
  13.     {
  14.         document.getElementById(i.toString()).appendChild(dv);
  15.     }
  16. }
  17.  
  18.  
Gives the following error:
Error: document.getElementById(i.toString()) has no properties

Why doesn't the tag have any properties? By setting the id with "dv.setAttribute("id",i.toString());" it should be setting the id to the number in the loop. Should I be using something else other than append child? I'm just trying to create the div tag after the one before it...
Oct 31 '07 #1
Share this Question
Share on Google+
2 Replies


P: 9
I want it it to look like this:
[HTML]
<div id="days">
<div id="1"></div>
<div id="2"></div>
<div id="3"></div>
<div id="4"></div>
<div id="5"></div>
<div id="6"></div>
...etc
<div id="12"></div>
</div>
[/HTML]
Oct 31 '07 #2

100+
P: 428
I see two separate problems.

You are trying to append your divs to themselves, after 0.

Plus,an id cannot begin with a digit.
Nov 1 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.