HTML | DOM nextElementSibling Property

The nextElementSibling property is used to return the immediate next element of the specified element, in the same tree level or null if the specified element is the last one in the list. It is a read-only property.
It differs from the nextSibling property as nextSibling returns next sibling node as an element node, a text node or a comment node, while nextElementSibling returns the next sibling as an element node (i.e it ignores text and comment nodes).

Syntax:

node.nextElementSibling

Return value: This property returns a next sibling of the specified element or null if the current element has no next sibling.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
      DOM nextSibling Property
    </title>
</head>
  
<body style="text-align: center">
  
    <h2 style="color:green;">
      DOM nextElementSibling Property
    </h2>
  
    <h4 id="h42">Web Languages:</h4>
    <select size="4">
        <option>HTML</option>
        <option id="Select">CSS</option>
        <option>JAVA SCRIPT
        </option>
        <option>XML</option>
    </select>
  
    <br>
    <br>
  
    <button onclick="geek()">Next of CSS</button>
  
    <br>
    <br>
  
    <p id="p" style="margin:auto; width: 40%"></p>
  
    <script>
        function geek() {
            var a = 
                document.getElementById("Select").nextElementSibling;
            document.getElementById("p").innerHTML = a.text;
            document.getElementById("p").style.color = "white";
            document.getElementById("p").style.background = "green";
        }
    </script>
</body>
  
</html>

chevron_right


Output:
Before clicking the button:
nextElementSibling
After clicking the button:
nextElementSibling
Note: Don’t put whitespace between two sibling elements, else the result will be “undefined”.

Supported Browsers: The browser supported by nextElementSibling property are listed below:

  • Google Chrome 2.0
  • Internet Explorer 9.0
  • Firefox 3.5
  • Opera 10.0
  • Apple Safari 4.0


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.