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 trial

JavaScript DOM Scripting By Example Editing and Filtering Names Filter Invitees Who Have Not Responded

Marcos Raj
PLUS
Marcos Raj
Courses 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.

1 Answer

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.

Best regards.