The HTML DOM nextElementSibling property returns the element node which is immediately following specified element node.
NOTE: This property does ignore text and comment nodes.
Following is the syntax −
Returning nextElementSibling node
nodeList.nextElementSibling
Let us see an example of HTML DOM nextElementSibling property −
Example
<!DOCTYPE html> <html> <head> <title>HTML DOM nextElementSibling</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-nextElementSibling</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> </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.nextElementSibling.textContent; } </script> </body> </html>
Output
Before clicking ‘Get Sale Day’ button −
After clicking ‘Get Sale Day’ button −