Liliana797979

add / delete - js advanced

Oct 8th, 2021
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function addItem() {
  2.     // Select input field and read value
  3.     const input = document.getElementById("newItemText");
  4.     console.log(input);
  5.     // Create new <li> element and set its content to input value
  6.    const liElement = document.createElement("li");
  7.    liElement.textContent = input.value;
  8.  
  9.    // create delete button
  10.    const button = document.createElement('a');
  11.    button.href = '#';
  12.    button.textContent = [Delete];
  13.     button.addEventListener('click', removeElement);
  14.    liElement.appendChild(button);
  15.     // Select <ul>  and append new <li> element
  16.     document.getElementById("items").appendChild(liElement);
  17.     // Nice-to-have: clear input field
  18.     input.value = "";
  19.  
  20.     function removeElement(ev) {
  21.         const parent = ev.target.parentNode
  22.         parent.remove();
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment