The HTML DOM nextSibling property returns the element node which is immediately following specified element node.
NOTE: This property does not ignore text and comment nodes.
Following is the syntax −
Returning nextSibling node
nodeList.nextSibling
Let us see an example of HTML DOM nextSibling property −
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM nextSibling</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
ul{
width: 30%;
margin: 0 auto;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>HTML-DOM-nextSibling</legend>
<h3>Grocery Store</h3>
<ul>
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
<li>Thursday</li>
<li>Friday</li>
<li id="Today">Saturday</li><li>Sunday</li>
//Do not add whitespace between <li> elements as lastSibling does not ignores whitespaces
</ul>
<input type="button" onclick="getTimmings()" value="Get Sale Day">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var dayToday = document.getElementById("Today");
function getTimmings() {
divDisplay.textContent = 'Sale on last day of week: '+dayToday.nextSibling.textContent;
}
</script>
</body>
</html>Output
Before clicking ‘Get Sale Day’ button: −

After clicking ‘Get Sale Day’ button −
