JavaScript previousSibling Propety Tutorial

In this section, we will learn what the previousSibling property is and how to use it in JavaScript.

What is Element previousSibling Propety in JavaScript?

The JavaScript Element previousSibling property is used to get a reference to the previous sibling of the current node (the one that invoked this property).

For example, if you have a list with three <li> elements with the values “Item 1” , “Item 2”, and “Item 3” set for each item respectively, then calling the `nextSibling` property on the last item will return the second item that has the value “Item 2”.

Note: calling this property can return the previous sibling no-matter if it’s an Element-node, Text-node, or Comment-node, etc. But if we want to get the previous sibling of a node that should be of type Element-node, we can then use previousElementSibling property instead.

JavaScript previousSibling Propety Syntax:

element.previousSibling;

JavaScript previousSibling Propety Input Value

The previousSibling property is read-only and so we can’t assign a value to it.

JavaScript previousSibling Propety Return Value

The return value of this property is a reference to a node that is the previous sibling of the current node that invoked this property.

Note: if there’s no sibling, the return value of this property will be null.

Example: using previousSibling Propety in JavaScript

See the Pen using previousSibling Propety in JavaScript by Omid Dehghan (@odchan1) on CodePen.

Leave a Reply