I am trying to learn stuff I was used to do in jQuery do in plain JavaScript.
I have example I found on the internet to solve and it really gave me hard time.
I am trying to remove parent div.single
on click on button.remove
.
Here is the code;
var btn = document.getElementsByClassName('remove')
for (var i = 0; i < btn.length; i++) {
btn[i].addEventListener('click',function (e) {
e.parentNode.parentNode.removeChild(e.parentNode);
} , false);
}
<div class="single">
<img src="http://via.placeholder.com/150x150">
<button type="button" class="remove">X1</button>
</div>
<div class="single">
<img src="http://via.placeholder.com/150x150">
<button type="button" class="remove">X2</button>
</div>
<div class="single">
<img src="http://via.placeholder.com/150x150">
<button type="button" class="remove">X3</button>
</div>
<div class="single">
<img src="http://via.placeholder.com/150x150">
<button type="button" class="remove">X4</button>
</div>
I am getting error e.parentNode is undefined
.
Here is the jQuery code which does the same I am after.
$(document).ready(function() {
$(document).on('click', '.remove', function () {
$(this).parent('.single').remove()
})
})
Thanks for any answers.
To remove the parent element without removing its children, call the replaceWith () method on the parent element passing it the child nodes as a parameter, e.g. parent.replaceWith (...parent.childNodes). The replaceWith method replaces the element with a set of Node objects.
As you might know, the DOM does not supports removing an element directly. When removing an element with JavaScript, you must go to its parent first instead.
function delete_row (e) { e.parentNode.parentNode.parentNode.removeChild (e.parentNode.parentNode); } You can now use node.remove () to remove the whole element so in your case you'd do
P. S. Beside these new methods, don’t forget about the querySelector () as well. That’s another cool JavaScript method which is very useful to select an element by class name for example. All these combined, provide a very good support for daily debugging and personal projects without using any framework.
To remove parent node as short way:
<div class="single">
<img src="http://via.placeholder.com/150x150">
<button type="button" onclick="return this.parentNode.remove();" class="remove">X</button>
</div>
You need to get the element reference from the event object(currentTarget/target)
Note: All the modern browser's have support for Node.remove()
var btn = document.getElementsByClassName('remove')
for (var i = 0; i < btn.length; i++) {
btn[i].addEventListener('click', function(e) {
e.currentTarget.parentNode.remove();
//this.closest('.single').remove() // in modern browsers in complex dom structure
//this.parentNode.remove(); //this refers to the current target element
//e.target.parentNode.parentNode.removeChild(e.target.parentNode);
}, false);
}
<div class="single">
<img src="http://via.placeholder.com/150x150">
<button type="button" class="remove">X1</button>
</div>
<div class="single">
<img src="http://via.placeholder.com/150x150">
<button type="button" class="remove">X2</button>
</div>
<div class="single">
<img src="http://via.placeholder.com/150x150">
<button type="button" class="remove">X3</button>
</div>
<div class="single">
<img src="http://via.placeholder.com/150x150">
<button type="button" class="remove">X4</button>
</div>
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