JavaScript – Get Next Sibling of an HTML Element

To get the next sibling of a specific HTML Element, using JavaScript, get reference to this HTML element, and read the nextElementSibling property of this HTML Element.

nextElementSibling property returns the element immediately following this HTML element.

In the following example, we will get the next sibling of the HTML Element, where the HTML element is selected by id "myElement". We shall change the font color of the next sibling.

example.html

Try this html file online, and click on the Click Me button. The script gets the next sibling of the HTML Element #myElement, and changes its color to red.

JavaScript - Get Next Sibling of an HTML Element
ADVERTISEMENT

Conclusion

In this JavaScript Tutorial, we learned how to get the next sibling element of an HTML Element, using JavaScript.