Reputation: 23
I'm new to JavaScript and I'm writing a kinda expenses management app. I'm stuck with writing a function which can remove the parentNode and the respective children wherever it's called.
I have a remove button next to all of my Items in my expenses list. BTW I've already tried using the this keyword to get the parent node but it returns undefined. My code:
function remover(){
var x = this.parentNode.parentNode;
}
Upvotes: 2
Views: 56
Reputation: 40768
Pass in that target element as an argument to the function, then use .parentElement
to access the parent element and simply apply .remove()
to remove the parent element from DOM.
function remove(that) {
that.parentElement.remove();
}
<div class="parent">
<div class="child" onclick="remove(this)">Click me to remove my parent!</div>
</div>
Upvotes: 1