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:
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.