parentNode property

The parentNode property contains the parent element. There is also an almost identical parentElement property. Differences: for the html tag, the parentNode property returns document, while parentElement returns null.

Syntax

javascriptelement.parentNode;

Example

Let's get the parent of the #elem element and display its id:

html<div id="parent">
  <p id="elem"></p>
</div>
javascriptlet elem = document.querySelector('#elem');
let id = elem.parentNode.id;

console.log(id);

The code execution result:

javascript'parent'

See also