Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any way to get the parent’s id?

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading