Deal with null sibling value in JavaScript

Description

The following code shows how to deal with null sibling value.

Example


<!DOCTYPE html>
<html>
<body>
<div id="div1">
<p>Hello <b>World!</b></p>
<ul>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ul><!-- w  w  w  .  j  av  a2 s . c om-->
</div>
<p>Hello <b>World!</b></p>
<script type="text/javascript">
var div = document.getElementById("div1");
var node = div.previousSibling;
if (node.nextSibling === null){
document.writeln("Last node in the parent's childNodes list.");
} else if (node.previousSibling === null){
document.writeln("First node in the parent's childNodes list.");
}
</script>

</body>
</html>

Click to view the demo

The code above generates the following result.

Deal with null sibling value in JavaScript
Home »
  Javascript Tutorial »
    DOM »
      Get
Javascript Tutorial Get
Access data attributes in HTML element in J...
Behave differently for different element id...
Call getElementById for duplicate id in Jav...
Clone a HTML element and output its inner h...
Clone a node in JavaScript
Compare node type to a numeric value in Jav...
Compare the value of innerText and innerHTM...
Convert NodeList to an Array in JavaScript
Convert a NodeList to an array in Internet ...
Create a HTML element in JavaScript
Create new HTML element and set its inner t...
Deal with null sibling value in JavaScript
Deal with the single child with lastChild i...
Get all attributes for a HTML element in Ja...
Get attribute from a HTML element in JavaSc...
Get child node by index in JavaScript
Get dir from element in JavaScript
Get disabled from element in JavaScript
Get element array by class name in JavaScri...
Get element array length returned by getEle...
Get element by id in JavaScript
Get elements by tag name with getElementsBy...
Get hidden from element in JavaScript
Get id from element in JavaScript
Get lang from element in JavaScript
Get tag name from element in JavaScript
Get tags by name using getElementsByName fu...
Get the first child for an element in JavaS...
Get the inner html for a HTML element in Ja...
Get the inner text for a HTML element in Ja...
Get the nodeName and nodeValue in JavaScrip...
Get the outer html for a HTML element in Ja...
Get the parent node in JavaScript
Get the previous sibling and next sibling i...
Get the reference to entire document in Jav...
List attribute for a HTML element by name a...
Loop through the array returned by getEleme...
Reference a button directly using its id in...
Show nodes are stored in a NodeList in Java...
Use length to get the last child in JavaScr...