From the course: JavaScript: Enhancing the DOM
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Working with methods to get parent, child, and sibling nodes
From the course: JavaScript: Enhancing the DOM
Working with methods to get parent, child, and sibling nodes
- [Instructor] Now that you understand the DOM family tree, let's explore the methods to access parent, child, and sibling nodes. The terms parent, child, and siblings are relative terms. In order to get a parent element, we first need to know what element we are talking about. So we would have to select that element first. This will give us a JavaScript object and we can use a specific property to find a parent of that node represented by the object. So for example, we could say document.getElementById child, assuming there's an element with the ID child. To get the parent of an element, we use the parentNode property. So for example, document.getElementByIdchild.parentNode. This will return the parent element of the element with the ID child. Here's a webpage. And on this webpage, you can see we have an Employee Count ID on line 16. It's a span and the span is inside a paragraph. Here's a JavaScript code we could use to access that parent node. So we first select the Employee…