HTML DOM Element nextElementSibling attribute

Definition and Usage

nextElementSibling The attribute returns the next element at the same tree level.

nextElementSibling The attribute is read-only.

See also:

previousElementSibling attribute

children property

firstElemenChild property

lastElementChild property

childElementCount property

HTML nodes and elements

In HTML DOM(Document Object Model), an HTML document is a collection of nodes (or without) that have child nodes.

NodeRefers to element nodes, text nodes, and comment nodes.

ElementWhitespace between elements is also a text node.

While elements are just element nodes.

Siblings and element siblings

SiblingsAre 'brothers' and 'sisters'.

SiblingsAre nodes that have the same parent node (in the same childNodes list).

Element siblingsAre elements that have the same parent element (in the same children list).

Child nodes and child elements

childNodes ReturnsChild nodes(Element nodes, text nodes, and comment nodes).

children ReturnsChild elements(Not text and comment nodes).

nextSibling and nextElementSibling

nextSibling Returns the nextNode(Element node, text node, or comment node). Whitespace between elements is also a text node.

nextElementSibling Returns the nextElement(Does not return text and comment nodes).

previousSibling and previousElementSibling

previousSibling Returns the previousNode(Element node, text node, or comment node). Whitespace between elements is also a text node.

previousElementSibling Returns the previousElement(Does not return text and comment nodes).

Example

Returns the HTML content of the next sibling:

let text = element.nextElementSibling.innerHTML;

Try it yourself

Syntax

element.nextElementSibling

Return value

Type Description
Element Next sibling element.
null If there is no next sibling.

Browser support

element.nextElementSibling It is a DOM Level 3 (2004) feature.

All browsers fully support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support