Advertisement
CR7CR7

shoppingList

Jun 8th, 2023
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function addEventListenersToElement(element) {
  2.   element.addEventListener("click", () => {
  3.     element.classList.toggle("done");
  4.   });
  5.  
  6.   var deleteButton = element.querySelector("button");
  7.   deleteButton.addEventListener("click", () => {
  8.     element.remove();
  9.   });
  10. }
  11.  
  12. function addEventListenersToListElements() {
  13.   var liArray = document.querySelectorAll("li");
  14.   liArray.forEach(addEventListenersToElement);
  15. }
  16.  
  17. function createListElement() {
  18.   var li = document.createElement("li");
  19.   li.appendChild(document.createTextNode(input.value + " "));
  20.  
  21.   var deleteButton = document.createElement("button");
  22.   deleteButton.appendChild(document.createTextNode("Delete"));
  23.   li.appendChild(deleteButton);
  24.  
  25.   ul.appendChild(li);
  26.   input.value = "";
  27.  
  28.   addEventListenersToElement(li);
  29. }
  30.  
  31. // Call addEventListenersToListElements() once initially
  32. addEventListenersToListElements();
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement