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

Dimitris Karatsoulas
Dimitris Karatsoulas
5,788 Points

How to remove a specific item list.

So, I was thinking it would be a cool feature and a nice exercise to have any of the list items removed just by writing them in the input and clicking on a "Remove Item" button. Below is my NOT WORKING code. I have entered a for loop to run through the <li> array and an if condition to check and find the right value. All variables etc are the same as in Guil's example except "addItemInput" that was altered to "itemInput" since it would serve both adding and removing items puposes. Everything else in the code is just like in the example. I just altered the removeItemButton code.

Any ideas on how to make this work?

removeItemButton.addEventListener("click", () => {
    let ul = document.getElementsByTagName("ul")[0];
    let li = document.querySelector("li");
    for (let i = 0; i < li.length; i++) {
        if (li[i] === itemInput.value) {
            ul.removeChild("li[i]");
        }
    }
    itemInput.value = "";
});

ul.removeChild(li[i]); might work? I'm not seeing the entire code, but right now with ul.removeChild("li[i]") I think you are telling JavaScript to remove an HTML element named li[i] (which doesn't exist)

Play around with that. :) cheers!

2 Answers

here is the correct snippet:

removeItemButton.addEventListener("click", (e) => { let target = e.target.value; let ul = document.getElementsByTagName("ul")[0]; let li = document.querySelectorAll("li"); alert(itemInput.value); for (let i = 0; i < li.length; i++) { if (li[i].innerText === itemInput.value) { ul.removeChild(li[i]); } } itemInput.value = ""; });

AND a link to see it working in action. I will remove this link in a day or so. https://codepen.io/RayThomas1GTU/pen/jOOpqdV?editors=0010

Dimitris Karatsoulas
Dimitris Karatsoulas
5,788 Points

That works like a charm, ".innerText" did all the work, plus the "querySelectorAll". Thank you very much my friend.

Dimitris Karatsoulas
Dimitris Karatsoulas
5,788 Points

Thank you for your response, guess you're right. Seems like it needs some more work to be done though.

Here's my full JavaScript code:

const toggleList = document.getElementById("toggleList");
const listDiv = document.querySelector(".list");
const descriptionInput = document.querySelector("input.description");
const descriptionP = document.querySelector("p.description");
const descriptionButton = document.querySelector("button.description");
const itemInput = document.querySelector("input.itemInput");
const addItemButton = document.querySelector("button.addItemButton");
const removeItemButton = document.querySelector("button.removeItemButton");


toggleList.addEventListener("click", () => {
    if (listDiv.style.display === "none" ) {
        toggleList.textContent = "Hide List";
        listDiv.style.display = "block";
    } else{
        listDiv.style.display = "none";
        toggleList.textContent = "Show List";
    }    
});

descriptionButton.addEventListener("click", () =>{
    descriptionP.innerHTML = descriptionInput.value + ":";
    descriptionInput.value = "";
} );


addItemButton.addEventListener("click", () => {
    let ul = document.getElementsByTagName("ul")[0];
    let li = document.createElement("li");
    li.textContent = itemInput.value;
    ul.appendChild(li);
    itemInput.value = "";
});

removeItemButton.addEventListener("click", () => {
    let ul = document.getElementsByTagName("ul")[0];
    let li = document.querySelector("li");
    for (let i = 0; i < li.length; i++) {
        if (li[i] === itemInput.value) {
            ul.removeChild(li[i]);
        }
    }
    itemInput.value = "";
});

and my HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript and the DOM</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <h1 id="myHeading">JavaScript and the DOM</h1>
    <p>Making a web page interactive</p>
    <button id ="toggleList">Hide List.</button>
    <div class="list">
      <p class="description">Things that are of color purple.</p>
      <input type="text" class="description">
      <button class="description">Change list description.</button>
      <ul>
        <li>grapes</li>
        <li >amethyst</li>
        <li>lavender</li>
        <li>plums</li>
      </ul>
      <input type="text" class="itemInput">
      <button class="addItemButton">Add Item</button>
      <button class="removeItemButton">Remove Item</button>
    </div>
    <script src="app.js"></script>
  </body>
</html>