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!
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 trial
Marcos RajCourses Plus Student 5,979 Points
What if I want to call the nth child of element from the LI?
const span = li.firstElementChild; input = document.createElement('input'); type = 'text'; value = span.textContent; insertBefore(input, span); li.removeChild(span); target.textContent = 'save';
in this we are replacing input tag from the span tag, and span tag is in the first place.
What if i want something to replace which is neither in the first place nor in the last place, but somewhere in the middle?
element.insertBefore() // with this method can i insert more than one item at time.
Serhii Lihus6,352 Points
Hi there Marcos!
You can simply combine css selector inside querySelector. As for example:
<ul id="list"> <li>one</li> <li>two</li> <li>three</li> <li>four</li> </ul>
var element = document.querySelector('#list li:nth-child(2)'); console.log(element);
This will save from to many unnecessary lines of code.