nextElementSibling property

The nextElementSibling property contains the next element in the same parent. If there is no such element, null is returned.

Syntax

javascriptelement.nextElementSibling;

Example

The element #elem is given. Let's display the text of his neighbor from below:

html<p id="elem">elem</p>
<p>sibling</p>
javascriptlet elem = document.querySelector('#elem');
let text = elem.nextElementSibling.textContent;

console.log(text);

The code execution result:

javascript'sibling'

Example

If there is no bottom neighbor or it is located not in the parent of our element, null is also returned:

html<div>
  <p id="elem">elem</p>
</div>
<p>sibling</p>
javascriptlet elem = document.querySelector('#elem');
console.log(elem.nextElementSibling);

The code execution result:

javascriptnull

See also