HTML DOM Element nextSibling attribute

Definition and Usage

nextSibling The attribute returns the next node at the same tree level.

nextSibling Returns a Node object.

nextSibling The attribute is read-only.

Note

nextSibling Returns the next sibling node: element node, text node, or comment node.

The whitespace between elements is also a text node.

Alternative:

nextElementSibling Property - nextElementSibling Returns the next sibling element (ignoring text and comment).

See Also:

previousSibling Property

firstChild Property

lastChild Property

childNodes Property

hasChildNodes() Method

HTML Node and Element

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

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

ElementThe whitespace between them is also a text node.

while elements are just element nodes.

Sibling vs Element Sibling

Siblingis "brother" and "sister".

Siblingis a node that has the same parent node (in the same childNodes in the list).

Element Siblingis an element that has the same parent element (in the same children in the list).

Child Nodes vs Child Elements

childNodes ReturnsChild Node(Element node, text node, and comment node).

children ReturnsChild Element(Instead of text and comment nodes).

nextSibling vs nextElementSibling

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

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

previousSibling vs previousElementSibling

previousSibling Returns the previousNode(Element node, text node, or comment node). The 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 of the list item:

document.getElementById("item1").nextSibling.innerHTML;

Try It Yourself

Syntax

element.nextSibling

or

node.nextSibling

Return Value

Type Description
Node The next sibling of the element.
null If there is no next sibling.

Browser Support

element.nextSibling It is a DOM Level 1 (1998) 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