
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM Element parentNode Property
The HTML DOM Element parentNode property is used to access the immediate parent node of a particular node within the HTML document. It returns the parent node of the specified node as a Node object.
If the specified element is the root element in the document, it will return null.
Syntax
Following is the syntax of the HTML DOM Element parentNode property −
element.parentNode;
Parameters
Since this is a property, it will not accept any parameter.
Return Value
This property returns the immediate parent node of the element. If no parent node exists it returns null.
Example 1: Accessing Parent Node of DIV Element
The following program demonstrates the usage of the HTML DOM Element parentNode property. It accesses and displays the parent node of <p> element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML parentNode Property</title> </head> <body> <h3>HTML DOM Element parentNode Property</h3> <p>Click the button to see the parent node of a p element.</p> <div id="parent"> <p>This is a paragraph inside a div.</p> </div> <button onclick="showParent()">Show Parent</button> <hr> <div id="output"></div> <script> function showParent() { var cel = document.querySelector('#parent p'); var pn = cel.parentNode; var otd = document.getElementById('output'); otd.textContent = `Parent Node: ${pn.tagName}`; } </script> </body> </html>
The above program displays the parent node of the "p" element.
Example 2: Iterating through the Parent Nodes
Here is another example of using the HTML DOM Element parentNode property. We use this method to iterate through each parent node −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element parentNode</title> </head> <body> <h3>HTML DOM Element parentNode Property</h3> <p>This example iterates through the parent element.</p> <ul id="myList"> <li>Item 1 <ul> <li>Subitem 1</li> </ul> </li> </ul> <button onclick="showParents()">Show Parents</button> <div id="ot"></div> <script> function showParents() { var subitem = document.querySelector('#myList li ul li'); var parents = []; var curr = subitem; while (curr.parentNode) { parents.push(curr.parentNode.tagName); curr = curr.parentNode; } var ot = document.getElementById('ot'); ot.textContent = `Parent Nodes: ${parents.join(' > ')}`; } </script> </body> </html>
The above program iterates through each parent node one by one.
Example 3: Accessing the Parent Node of the Root Element
In the example below we are using the parentNode property
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
parentNode | Yes | Yes | Yes | Yes | Yes |