Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialVic Mercier
3,276 PointscreateElement
If I have two ul and I want to add a li tag in the first ul, what should I do?(With the createElement method)
2 Answers
Steven Parker
231,269 PointsYou'll need another method for that.
The createElement method brings a new element into existence, but does not attach it to your document. For that, you'll need something like appendChild.
Your video link shows that you're just a couple of steps away from where this will be explained. I'd suggest you keep going and you'll see examples of this after a few more videos.
Abraham Juliot
47,353 PointsYou can select the first ul by giving it a unique id and then using the documents getElementById method, or you can use the documents querySelector method and use a css selector to get that specific tag:
- "ul:first-of-type" or "ul:nth-of-type(1)"
With the createElement method, you need to create your li tag and then append or prepend it to the ul list using the appendChild or insertBefore method:
- ulTag.insertBefore(liTag, ulTag.firstChild);
- ulTag.appendChild(liTag);
Vic Mercier
3,276 PointsVic Mercier
3,276 PointsSorry to asked you this stupid question.It was because I haven't done the next lesson yet. I apologize again and thanks for your answer!
Steven Parker
231,269 PointsSteven Parker
231,269 PointsNo problem. You just had a burst of enthusiastic eagerness. I can relate.