Javatpoint Logo
Javatpoint Logo

PreviousSibling Property in Javascript

The previousSibling property serves to get the previous or earlier node of the given node as a Node object. If the given node is the first item in the list, then the node object shows null.

The previousSibling property displays the element that came before the one you specified on the same level of the tree. The property is used to display previousSibling node, and it needs to return the preceding sibling node as a text node, element node, or comment node. This property can be read only for the web page.

Note that the children's property works to get back all of an element's children. We can get the previous sibling node in the alert, console, and innerHTML function.

Syntax

The following syntax gives the previous sibling node of the list.

Return value

  • This property gives a node's previous sibling as an element.
  • This property gives a null if the previous sibling node does not available.

Note Don't put whitespace between two elements already close to each other, or the result will be "undefined."

Examples

The following example shows the output of the multiple values or nodes using previousSibling property.

Example 1

The basic previousSibling property in the javascript example shows below. Here we can get the previous sibling node of the second node.

Output

The image displays the previous sibling node's value as an output.

PreviousSibling Property in Javascript

Example 2

The basic previousSibling property shows output in the console log function. Here we can get the previous sibling node of the third node.

Output

The image shows the previous sibling node's value as an output.

PreviousSibling Property in Javascript

Example 3

The basic previousSibling property in javascript with condition example shows below. Here we can get the previous sibling node's available value. The property displays the list value if the previous sibling node is available. The property shows a null value if the previous sibling nodes are unavailable.

Output

The image displays the previous sibling node's value as an output. The output is null because the previous sibling node is unavailable.

PreviousSibling Property in Javascript

Example 4

The basic previousSibling property in javascript with condition example shows below. Here we can get the previous sibling node's available value if the list gives without space. The list tag place space then the data shows undefined.

Output

The image displays an undefined value (space in the list ) as an output.

PreviousSibling Property in Javascript

Example 5

The basic previousSibling property in javascript with condition example shows below. Here we can get the previous sibling nodes of the first, third and last node simultaneously.

Output

The image displays the previous sibling node's value as an output. The output is null because the previous sibling node is unavailable.

PreviousSibling Property in Javascript

Example 6

The previous sibling node property is used for all tags without whitespace. It displays data as a list but does not necessarily list tags.

Output

The image displays the previous sibling nodes of the second nodes.

PreviousSibling Property in Javascript

Supported Browsers

Here is a list of the browsers that the previousSibling property works with:

  • Google Chrome version 1 and up
  • Edge 12 or higher and
  • Internet Explorer 5.5 or higher
  • Firefox 1 or higher
  • Opera 12.1 or higher
  • Safari 1 or higher

Conclusion

The previousSibling node finds the earlier or before node of the required node without whitespace in the list tag. It is a basic and required JavaScript function for the list type of data or array type of information.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA