nextElementSibling property
The nextElementSibling property
contains the next element in the same
parent. If there is no such element,
null is returned.
Syntax
element.nextElementSibling;
Example
The element #elem is given. Let's
display the text of his neighbor from below:
<p id="elem">elem</p>
<p>sibling</p>
let elem = document.querySelector('#elem');
let text = elem.nextElementSibling.textContent;
console.log(text);
The code execution result:
'sibling'
Example
If there is no bottom neighbor or it
is located not in the parent of our
element, null is also returned:
<div>
<p id="elem">elem</p>
</div>
<p>sibling</p>
let elem = document.querySelector('#elem');
console.log(elem.nextElementSibling);
The code execution result:
null
See also
-
the
previousElementSiblingproperty
that contains a previous element