PreviousSibling Property in JavascriptThe 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
Note Don't put whitespace between two elements already close to each other, or the result will be "undefined."ExamplesThe 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. 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. 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. 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. 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. 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. Supported BrowsersHere is a list of the browsers that the previousSibling property works with:
ConclusionThe 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. Next TopicJavaScript sessionStorage |