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 JavaScript and the DOM (Retiring) Making Changes to the DOM Removing Nodes

Remove the list item element stored in firstListItem from the DOM.

Hi everybody,

I have some problemi with this question. I tried in this way but it tells me it's wrong:

'''li.removeChild(firstListItem);'''

Can you help me? Thanks

2 Answers

Simon Coates
Simon Coates
28,694 Points

If this is for the challenge (your post was linked to a video, not a challenge), try:

let myList = document.querySelector("ul");
let firstListItem = myList.querySelector("li");
myList.removeChild(firstListItem);
Simon Coates
Simon Coates
28,694 Points

Demo:

<!DOCTYPE html>
<html>
<body>
<p>An unordered list:</p>
<ul>
  <li id = "remove">Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ul>
<script>
var child = document.getElementById("remove");
var parent = child.parentElement;
parent.removeChild(child);
</script>
</body>
</html>

produces:

An unordered list:
Tea
Milk

Finding:removeChild should be okay as long as the elements are populated and have the correct relationship. You might want to look at how you're populating the firstListItem and li variables, or ensure that the html structure reflects how you select these.