Element.prototype.parentElement
Returns the parent Element of the specified element, or null if the element has no parent or the parent is not an Element
Syntax
element.parentElementReturn Value
The parent Element, or null
Examples
const child = document.querySelector('.child')!
const parent = child.parentElement
console.log(parent?.tagName)function getAncestors(el: HTMLElement): HTMLElement[] {
const ancestors: HTMLElement[] = []
let current = el.parentElement
while (current) {
ancestors.push(current)
current = current.parentElement
}
return ancestors
}const input = document.querySelector('input')!
const formGroup = input.parentElement
formGroup?.classList.add('has-value')Understanding Element.prototype.parentElement
The Element.prototype.parentElement method in JavaScript returns the parent Element of the specified element, or null if the element has no parent or the parent is not an Element. It belongs to the Element object and is one of the most widely used methods for working with element values in modern JavaScript and TypeScript applications.
The method signature is element.parentElement. When called, it returns the parent element, or null. Understanding when and how to use parentElement() helps you write more expressive, readable code.
Common use cases for Element.prototype.parentElement include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dom-closest, dom-children, dom-firstelementchild, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Element.prototype.parentElement is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Element.prototype.closestTraverses the element and its parents toward the document root until it finds an element that matches the specified CSS selector
Element.prototype.childrenReturns a live HTMLCollection of the child elements of the element
Element.prototype.firstElementChildReturns the element's first child Element, or null if there are no child elements
More Element Methods
Other methods in the Element object
Related Tools
More Element Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.