Home > Blockchain >  How to get the a child's parent's ID in JavaScript
How to get the a child's parent's ID in JavaScript

Time:05-03

I need to get the child's parent's ID

Heres what I Mean:

<div  id="TheParent">
            <img  src="./index.png" />
            <div >
                <p >Sample Task</p>
                <p >Sample Desc</p>
            </div>


            <input type="checkbox"  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?

CodePudding user response:

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>

  • Related