NextSibling Property in JavascriptThe nextSibling property gets the next node of the given node as a Node object. If the given node is the last item in the list, then the node object gets null. The nextSibling property shows the element that came before the one you specify in the same level of the tree. This property can be read only for the web page. The property is used to display nextSibling node, and it needs to return the next sibling node as a text node, element node, or comment node. Note that the children's property works to get back all of an element's children. SyntaxThe following syntax gives the next sibling node of the list. node.nextSibling Return value
Note Don't put whitespace between two elements already close to each other, or the result will be "undefined."Supported BrowsersHere is a list of the browsers that the nextSibling property works with:
ExamplesThe following example gets the output of the multiple values or nodes using the nextSibling property. Example 1 The basic nextSibling property in the javascript example gets below. Here we can get the next sibling node of the second node. The output is displayed in the innerHTML, console, and alert functions. Output The image shows the next sibling node's value as an output. Example 2 The basic nextSibling property shows output in the console log function. Here we can get the next sibling node of the third node. Output The image shows the next sibling node's value as an output. Example 3 The basic nextSibling property in javascript with condition example gets below. Here we can get the next sibling node's available value. The property displays the list value if the next sibling node is available. The property gets a null value if the next sibling nodes are unavailable. Output The image shows the next sibling node's value as an output. The output gets a null value because the next sibling node is unavailable. Example 4 The basic nextSibling property in javascript with condition example gets below. Here we can get the next sibling node's available value if the list gives without space. The list tag place space than the data gets undefined. Output As an output, the image shows an undefined value (space in the list ). Example 5 The basic nextSibling property in javascript with condition example shows below. Here we can get the next sibling nodes of the first, third, and last node simultaneously. Output The image displays the next sibling node's value as an output. The output is null because the next sibling node is unavailable. Example 6 The next 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 next sibling nodes of the second nodes. ConclusionThe nextSibling property helps to find out the next element of the object from the list. We can use a list tag or other tag in the list format. We can use list objects and items without whitespace on the html page. Next TopicPreviousSibling Property in Javascript |