简体   繁体   中英

having trouble deleting specific html element with javascript

I'm having trouble deleting the specific element that the delete button is for, instead it deletes the last in the array, or the first with my previous code, any help is greatly appreciated. Vanilla js. Codepen: https://codepen.io/Adrw4/pen/LzvjLj

  idNum--;
  console.log(idNum);
  console.log(array);
  var node = document.getElementById(idNum);
  node.parentNode.removeChild(node);

Instead of relying on the ID of the elements, just pass in the event to the deleteTodoItem function on click event. Based on the knowledge of the DOM, you can find out the related li element and delete it.

function deleteTodoItem(event) {
  var node = event.target.parentNode;
  node.parentNode.removeChild(node);
};

Here's the updated codepen:

https://codepen.io/Nisargshah02/pen/OxGjOr?editors=1010

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM