I'm having a problem with my javascript. I want to create some kind of to-do list but I can't figure out how to delete an item.
When creating an item, it adds to the list with a delete button, but I don't know what to do for the item to delete itself when clicking on the button.
I have this on the addItem function
var item = document.createElement("li");
item.innerHTML = itemText + "<button class='delete'>Delete item</button>";
list.appendChild(item);
And this would be the function to delete the item, but I don't know what to put inside ...
function deleteItem() {
}
You have a couple of options:
You could attach it as a handler directly to the button, then use this.parentNode to access the parent element. To actually remove it, you'd probably want to go one step higher up the hierarchy (DOM4 defines a remove method on elements, but it may not be supported by older browsers). That would look like this:
var item = document.createElement("li");
var btn = document.createElement("button");
btn.className = "delete";
btn.innerHTML = "Delete item";
btn.addEventListener("click", deleteItem, false);
item.appendChild(btn);
list.appendChild(item);
// ...
function deleteItem() {
this.parentNode.parentNode.removeChild(this.parentNode);
}
Instead of that, though, I'd use event delegation: Hook click on the list, and if the click passed through one of these delete buttons, handle it then: Your code for adding the list item and button would be unchanged. In one place, you'd have this:
list.addEventListener("click", deleteItem, false);
and deleteItem would look like this:
function deleteItem(e) {
var btn = e.target;
// Since `button` elements can have elements inside them,
// we start with the element that was the target of the
// event and look to see if any the event passed through
// a `button class="delete"` on its way to the list.
while (btn && (btn.tagName != "BUTTON" || !/\bdelete\b/.test(btn.className))) {
btn = btn.parentNode;
if (btn === this) {
btn = null;
}
}
if (btn) {
// Yes, it did -- remove the button's parent from the list
// (`this` is the list, because that's what we hooked the
// event on)
this.removeChild(btn.parentNode);
}
}
Here's a live example of #2:
var list, item, n;
// Get the list, add a handler to it
list = document.getElementById("the-list");
list.addEventListener("click", deleteItem, false);
// Add items to it; you can do this at any time
for (n = 1; n <= 10; ++n) {
item = document.createElement("li");
item.innerHTML = "Item #" + n + "<button class='delete'>Delete item</button>";
list.appendChild(item);
}
// Handle clicks that might come through the delete button
function deleteItem(e) {
var btn = e.target;
while (btn && (btn.tagName != "BUTTON" || !/\bdelete\b/.test(btn.className))) {
btn = btn.parentNode;
if (btn === this) {
btn = null;
}
}
if (btn) {
this.removeChild(btn.parentNode);
}
}
<ul id="the-list"></ul>
function registerClickHandler () {
var x = document.querySelectorAll(".image");
for(var i = 0; i < x.length; i++) {
x[i].querySelector(".remove").onclick = function(){
this.parentNode.parentNode.removeChild(this.parentNode);
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With