HTML DOM Element previousElementSibling attribute

Definition and usage

previousElementSibling The attribute returns the previous element at the same tree level.

previousElementSibling The attribute is read-only.

See also:

nextElementSibling attribute

children attribute

firstElemenChild property

lastElemenChild property

childElementCount property

HTML nodes and elements

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 spaces between elements are also text nodes.

And elements are just element nodes.

Siblings vs element siblings

SiblingsAre 'brothers' and 'sisters'.

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

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

Child nodes vs child elements

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

children ReturnsChild elements(Not text and comment nodes).

nextSibling vs nextElementSibling

nextSibling Returns the nextNode(Element node, text node, or comment node). The spaces between elements are also text nodes.

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 spaces between elements are also text nodes.

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

Example

Returns the HTML content of the previous sibling:

let text = element.previousElementSibling.innerHTML;

Try it yourself

Syntax

element.previousElementSibling

Return value

Type Description
Element Previous sibling element.
null If there is no preceding sibling.

Browser support

element.previousElementSibling 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