'How to get the a child's parent's ID in JavaScript
I need to get the child's parent's ID
Heres what I Mean:
<div class="task" id="TheParent">
<img class="task-img" src="./index.png" />
<div class="Text-Container">
<p class="task-maintitle">Sample Task</p>
<p class="task-desc">Sample Desc</p>
</div>
<input type="checkbox" class="Check" id="check" onclick="Remove(this.id)" />
<!--Need this element's parent's id-->
</div>
I need to get it with this specifically because there are going to be many elements which are just duplicates.
Is there any way to get the parent's id?
Solution 1:[1]
You can get it with this.parentNode
I added this.parentNode.id for the demo purpose
function getParent(parenId) {
console.log(parenId)
}
<div id="parent-div">
<button onclick="getParent(this.parentNode.id)">
Test
</button>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
