Access a Parent Element With JavaScript or jQuery

Share this article

Often you’ll want your JavaScript functions to access parent elements in the DOM.

To accomplish this in JavaScript, try element.parentNode.

To do the same in jQuery, try element.parent().

Here’s a JavaScript example.

HTML:

<ul id="tabs">
<li class="firsttab"><a href="#">one</a></li>
<li class="secondtab"><a href="#">two</a></li>
</ul>

JavaScript:

function init() {
  var tablinks = document.getElementById('tabs').getElementsByTagName('a');
  for (var i = 0, j = tablinks.length; i < j; i++) {
    tablinks[i].onclick = doit;
  }
}
function doit() {
  alert(this.parentNode.className);
}
window.onload = init;

Frequently Asked Questions (FAQs) about Accessing Parent Elements in JavaScript and jQuery

What is the difference between parentElement and parentNode in JavaScript?

In JavaScript, both parentElement and parentNode are used to access the parent of a particular element. However, there is a subtle difference between the two. The parentNode property returns the parent node of an element as a Node object, which means it can return any type of node (element node, text node, comment node, etc.). On the other hand, the parentElement property returns the parent node as an Element object, which means it only returns element nodes. If the parent node is not an element node, parentElement will return null.

How can I access the parent element using jQuery?

In jQuery, you can use the parent() method to get the direct parent element of the selected element. For example, if you have an element with the id “child”, you can access its parent element like this: $(“#child”).parent(). This will return a jQuery object containing the parent element.

Can I access the grandparent or higher ancestor elements in JavaScript?

Yes, you can access the grandparent or any higher ancestor elements in JavaScript. You can do this by chaining the parentElement or parentNode properties. For example, to access the grandparent of an element, you can use element.parentElement.parentElement.

What if the parent element is not an HTML element in JavaScript?

If the parent node of an element is not an HTML element (for example, it could be a text node or a comment node), the parentElement property will return null. However, the parentNode property will still return the parent node, regardless of its type.

How can I get all ancestor elements of a particular element in jQuery?

In jQuery, you can use the parents() method to get all ancestor elements of a particular element. For example, $(“#child”).parents() will return a jQuery object containing all ancestor elements of the element with the id “child”.

Can I filter the parent elements I get with jQuery?

Yes, you can filter the parent elements you get with jQuery. Both the parent() and parents() methods can take a selector as an argument, and they will only return the parent elements that match this selector. For example, $(“#child”).parents(“div”) will only return the ancestor elements that are divs.

How can I get the closest ancestor element that matches a certain condition in JavaScript?

In JavaScript, you can use the closest() method to get the closest ancestor element that matches a certain condition. This method takes a selector as an argument and returns the closest ancestor element that matches this selector. For example, element.closest(“div”) will return the closest ancestor element that is a div.

What is the difference between the parents() and closest() methods in jQuery?

In jQuery, the parents() method returns all ancestor elements that match a certain condition, while the closest() method only returns the closest ancestor element that matches the condition. So if you only need the closest matching ancestor, use closest(). If you need all matching ancestors, use parents().

Can I access the parent element of a text node or a comment node in JavaScript?

Yes, you can access the parent element of a text node or a comment node in JavaScript. You can do this by using the parentNode property. This property will return the parent node of any type of node, including text nodes and comment nodes.

How can I check if an element has a parent in JavaScript?

In JavaScript, you can check if an element has a parent by using the parentElement or parentNode property. If the element has a parent, these properties will return the parent element or node. If the element does not have a parent, these properties will return null. So you can use a condition like if (element.parentElement) or if (element.parentNode) to check if an element has a parent.

Adam RobertsAdam Roberts
View Author

Adam is SitePoint's head of newsletters, who mainly writes Versioning, a daily newsletter covering everything new and interesting in the world of web development. He has a beard and will talk to you about beer and Star Wars, if you let him.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week