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

append a child as the first node

100+
P: 178
Hi All,

Im sure its possible, every time I create a new element and add it to a node it becomes the last child.
a=appendChild(object);


How do I append a child to be the first node?
Jul 8 '08 #1
Share this Question
Share on Google+
1 Reply


100+
P: 428
This code inserts a new p as the first child node of the first div in the document.
Expand|Select|Wrap|Line Numbers
  1. var who=document.createElement('p');
  2. var pa= document.getElementsByTagName('div')[0];
  3.  
  4. if(pa.firstChild) pa.insertBefore(who,pa.firstChild);
  5. else pa.appendChild(who);
Jul 8 '08 #2

Post your reply

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