All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

JavaScript Get Parent Element From Child

Last Updated : Mar 11, 2024

JavaScript Get Parent Element From Child

In this tutorial we will show you the solution of JavaScript get parent element from child, here we used querySelector() method and parentNode, classList statements for collect parent node by child help.

The parentNode property of the Node interface returns the parent of the specified node in the DOM tree.

The classList is a DOM property which allows for styling the CSS classes of an element.

The querySelector() method allows you to select the first element that matches one or more CSS selectors.

Step By Step Guide On JavaScript Get Parent Element From Child :-

Here we defined div element with class ‘wrapper’ as parent node then within that we defined button ‘Click Me’ id ‘btn’ as child of wrapper.

In script we collecting button element by querySelector() using id ‘btn’ and stored on variable ‘bttn’.

When we appends property ‘parentNode’ with child ‘bttn’ we gets parent value stored on variable ‘ele’ then printed result on console with the help fo classList.

<!DOCTYPE html>
        <title>GET PARENT ELEMENT FROM CHILD</title>
        <div class="wrapper">
            <button id="btn">Click Me</button>
            const bttn=document.querySelector('#btn');
            const ele=bttn.parentNode;
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined div element with class attribute ‘wrapper’, it will consider as parent then within that we defined button element ‘Click Me’ with id attribute ‘btn’ it is child for div element.
  7. In script we selecting button ‘btn’ by querySelector() method and stored on variable ‘bttn’. When we need parent element we have to append ‘parentNode’ property with child ‘bttn’ then we gets result of parent name that is stored on variable ‘ele’ and at last we printed result on console by console.log() method by classList with [0] position.
  8. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to get parent element from child using javascript.

When we executes program on browser we can see button ‘Click Me’ then now we have to open console by using shortcut ‘ctrl+shift+j’ then at right hand side console panel will open with result of parent name.

If you need to define any other element as child then you can modify it accordingly.

I hope this tutorial on JavaScript get parent element from child helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪