with pure javascript, I need to remove a li
on click the span
.
remove
I want to remove its div.Object.prototype.remove = function(){
this.parentNode.removeChild(this);
};
var lis = document.querySelectorAll('li');
var button = document.querySelectorAll('span');
for (var i = 0, len = lis.length; i < len; i++) {
button[i].addEventListener('click', remove, false);
}
<ul>
<li>List item one<span> remove</span></li>
<li>List item two<span> remove</span></li>
<li>List item three<span> remove</span></li>
<li>List item four<span> remove</span></li>
<li>List item five<span> remove</span></li>
<li>List item six<span> remove</span></li>
</ul>
To remove an element from the DOM onclick in JavaScript: Select the DOM element with a method like getElementById() . Add a click event listener to the element. Call the remove() method on the element in the event handler.
The remove() method removes an element (or node) from the document.
First, select the target element that you want to remove using DOM methods such as querySelector() . Then, select the parent element of the target element and use the removeChild() method.
Don't pollute Object
. You don't need this function in every object. Create an separate function and use remove()
, not removeChild()
.
The ChildNode.remove() method removes the object from the tree it belongs to.
But remove doesn't work in every browser. It is a new function. So I suggest you two solutions.
With remove()
var remove = function(){
this.parentNode.remove();
};
var lis = document.querySelectorAll('li');
var button = document.querySelectorAll('span');
for (var i = 0, len = lis.length; i < len; i++) {
button[i].addEventListener('click', remove, false);
}
<ul>
<li>List item one<span> remove</span></li>
<li>List item two<span> remove</span></li>
<li>List item three<span> remove</span></li>
<li>List item four<span> remove</span></li>
<li>List item five<span> remove</span></li>
<li>List item six<span> remove</span></li>
</ul>
With removeChild()
.
Why 2 parentNodes?
Because the first is the <span>
, but you need li
function remove() {
this.parentNode.parentNode.removeChild(this.parentNode);
}
var lis = document.querySelectorAll('li');
var button = document.querySelectorAll('span');
for (var i = 0, len = lis.length; i < len; i++) {
button[i].addEventListener('click', remove, false);
}
<ul>
<li>List item one<span> remove</span></li>
<li>List item two<span> remove</span></li>
<li>List item three<span> remove</span></li>
<li>List item four<span> remove</span></li>
<li>List item five<span> remove</span></li>
<li>List item six<span> remove</span></li>
</ul>
Try following solution.
Object.prototype.remove = function(){
this.parentNode.parentNode.removeChild(this.parentNode);
};
var lis = document.querySelectorAll('li');
var button = document.querySelectorAll('span');
for (var i = 0, len = lis.length; i < len; i++) {
button[i].addEventListener('click', remove, false);
}
<ul>
<li>List item one<span> remove</span></li>
<li>List item two<span> remove</span></li>
<li>List item three<span> remove</span></li>
<li>List item four<span> remove</span></li>
<li>List item five<span> remove</span></li>
<li>List item six<span> remove</span></li>
</ul>
It would be better to just define a simple function instead of changing the object
prototype, as @Satpal suggested.
function remove() {
this.parentNode.parentNode.removeChild(this.parentNode);
}
var lis = document.querySelectorAll('li');
var button = document.querySelectorAll('span');
for (var i = 0, len = lis.length; i < len; i++) {
button[i].addEventListener('click', remove, false);
}
<ul>
<li>List item one<span> remove</span></li>
<li>List item two<span> remove</span></li>
<li>List item three<span> remove</span></li>
<li>List item four<span> remove</span></li>
<li>List item five<span> remove</span></li>
<li>List item six<span> remove</span></li>
</ul>
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