0

I'm making a shopping list (using HTML, CSS, and JS) I want to write down stuff on my list and I want it to be removed if I click a trash bin icon. I already added a dynamically operated function of the trash bin icon and the icon is shown up. but if I click it, stuff is not disappeared.

Shopping list image

Error Message

const input = document.querySelector(".input");
const addItemBtn = document.querySelector("#addItemBtn");
const list = document.querySelector("ul");

// Add a stuff with a click btn or the Enter key
addItemBtn.addEventListener("click", () => {
  newElement();
});
document.addEventListener("keyup", (event) => {
  if (event.key === "Enter") newElement();
});

function newElement() {
  //add stuff
  let li = document.createElement("li");
  li.textContent = input.value;
  input.value = "";
  list.appendChild(li);

  //add a trash bin icon
  let removeBtn = document.createElement("i");
  removeBtn.setAttribute("class", "fa fa-trash");
  li.appendChild(removeBtn);
}

//Remove the stuff clicking a trash bin icon
let removeBtn = document.querySelector(".fa-trash");
removeBtn.addEventListener("click", () => {
  let li = document.querySelector("li");
  list.removeChild(li);
  list.removeChild(removeBtn);
});
body {
  background-color: #c1cfe6;
  display: flex;
  justify-content: center;
  flex-direction: column;
  margin: auto;
  width: 300px;
  padding-top: 20px;
}

h1 {
  text-align: center;
  background-image: linear-gradient(to bottom right, #ee9ca7, #ffdde1);
  margin: 0;
  font-size: 30px;
  color: white;
  padding-bottom: 10px;
}

.deco {
  background-color: azure;
  padding: 150px 0;
}


/* ul {
    }
    
    li {
    } */


/* Trash Bin */

.fa-trash::before {
  cursor: pointer;
  margin-left: 180px;
}

.input {
  display: inline-block;
  width: 100%;
  height: 30px;
  border: none;
  padding: 0;
  background-color: #e8edec;
}


/* Add button */

.fa-plus-circle {
  display: inline-block;
  width: 100%;
  height: 45px;
  text-align: center;
  font-size: 35px;
  padding-top: 5px;
  background-image: linear-gradient(to right, #ee9ca7, #ffdde1);
  cursor: pointer;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.1/css/all.css" integrity="sha384-xxzQGERXS00kBmZW/6qxqJPyxW3UR0BPsL4c8ILaIWXva5kFi7TxkIIaMiKtqV1Q" crossorigin="anonymous" />
<h1>Shopping List</h1>
<div class="deco">
  <ul></ul>
</div>
<input type="text" class="input" />
<i id="addItemBtn" class="fa fa-plus-circle" aria-hidden="true"></i>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
user
  • 25
  • 6
  • I made you a snippet. The error is very clearly explaining what is going on. You need to delegate from the closest static container – mplungjan Jul 15 '20 at 05:49
  • `document.querySelector(".deco").addEventListener("click",function(e) { const tgt = e.target; if (tgt.classList.contains("fa-trash")) tgt.closest("li").remove(); })` – mplungjan Jul 15 '20 at 05:53
  • Thanks for your help, I appreciate it! I didn't know Element.closest() – user Jul 15 '20 at 06:05
  • You can use parentElement instead for IE11 compatibility – mplungjan Jul 15 '20 at 06:06

0 Answers0