DOM Node.nextSibling Property
The nextSibling property returns the node immediately after a node in its parent, which may be a text node. previousSibling returns the one before.
Description
The read-only nextSibling property returns the node that immediately follows this one in its parent's child list, and previousSibling the one before. Like firstChild, they can return white-space text nodes, so nextElementSibling and previousElementSibling are usually what you want.
They return null at the end and start of the parent's children, so a loop such as for (let n = parent.firstChild; n; n = n.nextSibling) visits every child node, including text and comments.
Comments count as siblings too, so markup with HTML comments between elements gives yet more nodes to skip. When you only care about elements, the element-only versions are simpler and less fragile if the HTML formatting changes.
Syntax
node.nextSibling
node.previousSiblingValue
The next (or previous) node with the same parent, or null if there isn't one.
Example
Browser Support
The following information will show you the current browser support for the Node.nextSibling property. Hover over a browser icon to see the version that first introduced support for this property.
Desktop





Tablets & Mobile





