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 trialMarcos Raj
Courses Plus Student 5,979 PointsWhat if I want to select a particular children from the UL?
const lis = ul.children;
In the above code it will trigger all the UL children, but what if i want to trigger only the the first children? The Code I might has been written is as mentioned below:
const lis = ul.children[0];
Kindly correct me if it is wrong. Thanks in advance!
1 Answer
Serhii Lihus
6,352 PointsHi there Marcos!
First of all you can simply select all those children.
<ul id="list">
<li>one</li>
<li>two</li>
<li>three</li>
<li>four</li>
</ul>
// You can select them directly(all of them)
var li = document.querySelectorAll('#list li');
// only first
console.log('Show first element :' + li[0])
var length = li.length;
// show all
for(var i = 0; i < length; i++)
console.log(li[i]);
Or you can use children property. Html for this case remain the same.
// selecting ul
var ul = document.querySelector('#list');
// using children property (show last element of the list)
console.log(ul.children[ul.children.length-1]);
Best regards.