JavaScript’s Node.parentElement
read-only property returns the Node
’s parent element or null
if the Node
does not have a parent element.
Value
An Element
object or null
if the node does not have a parent element.
Examples
Here is an example of using the parentElement
property.
Suppose we have the following elements:
<div>
<p>You can do wonderful things with JavaScript.</p>
</div>
How can we change the background color of the parent element of the p
element with JavaScript?
Well, we can select the p
element with the querySelector()
method and use the parentElement
and style
properties:
let p = document.querySelector('p');
p.parentElement.style.backgroundColor = 'orange';
The HTMLElement.style
property returns a CSSStyleDeclaration
object containing the inline style of an element. The style can be modified by setting the object’s properties.
Browser compatibility
Chrome
|
Edge
|
Firefox
|
Internet Explorer
|
Opera
|
Safari
|
Chrome (Android)
|
Firefox (Android)
|
Opera (Android)
|
Safari (iOS)
|
Samsung Internet
|
WebView (Android)
|
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
parentElement |
1 | 12 | 9 | 9 | 7 | 1.1 | 18 | 9 | 10.1 | 1 | 1.0 | 4.4 |