Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. HTML Element .nextElementSibling / previousElementSibling

HTML Element .nextElementSibling / previousElementSibling

Since: DOM Level 1(1998)

A property for accessing sibling elements (adjacent elements that share the same parent) of an HTML element. You can navigate to the next or previous sibling element.

Syntax

// Get the next sibling element.
var next = element.nextElementSibling;

// Get the previous sibling element.
var prev = element.previousElementSibling;

Property List

PropertyDescription
nextElementSiblingReturns the next sibling element (HTML element). Returns null if no next sibling element exists.
previousElementSiblingReturns the previous sibling element (HTML element). Returns null if no previous sibling element exists.
nextSiblingReturns the next sibling node. This includes text nodes and comment nodes.
previousSiblingReturns the previous sibling node. This includes text nodes and comment nodes.

Sample Code

sample_nextElementSibling.html
<ul id="list">
	<li id="rei">Ayanami Rei</li>
	<li id="shinji">Ikari Shinji</li>
	<li id="asuka">Soryu Asuka</li>
	<li id="misato">Katsuragi Misato</li>
	<li id="kaworu">Nagisa Kaworu</li>
</ul>
<div id="accordion">
	<button class="acc-btn">NERV – Tokyo-3</button>
	<div class="acc-panel" style="display:none;">NERV is the organization that operates the Evangelion units.</div>
	<button class="acc-btn">About Angels</button>
	<div class="acc-panel" style="display:none;">The Angels range from the 3rd to the 18th.</div>
</div>
sample_nextElementSibling.js
// Pattern 1: Basic usage of nextElementSibling / previousElementSibling
var shinji = document.getElementById("shinji");
console.log(shinji.nextElementSibling.textContent);     // Outputs "Soryu Asuka".
console.log(shinji.previousElementSibling.textContent); // Outputs "Ayanami Rei".

// Pattern 2: null is returned for elements at the edge
var rei = document.getElementById("rei");
console.log(rei.previousElementSibling); // No previous sibling — returns null.
var kaworu = document.getElementById("kaworu");
console.log(kaworu.nextElementSibling);  // No next sibling — returns null.

// Pattern 3: Difference from nextSibling (includes text nodes)
console.log(rei.nextSibling.nodeType);        // Text node (newline) returns 3.
console.log(rei.nextElementSibling.nodeType); // HTML element returns 1.

// Pattern 4: Build an accordion menu
// Toggle the panel that immediately follows each button.
var accBtns = document.querySelectorAll(".acc-btn");
accBtns.forEach(function(btn) {
	btn.addEventListener("click", function() {
		var panel = btn.nextElementSibling; // Gets the div.acc-panel right after the button.
		if (panel.style.display === "none") {
			panel.style.display = "block";
		} else {
			panel.style.display = "none";
		}
	});
});

View sample page

Overview

element.nextElementSibling and element.previousElementSibling are properties that navigate to adjacent elements among siblings that share the same parent element. nextSibling and previousSibling can return text nodes and comment nodes, whereas nextElementSibling and previousElementSibling return only HTML elements, making them the safer choice for typical DOM manipulation.

Combining these properties with element.parentNode and element.children lets you move freely through the DOM tree. For example, you can use them to build an accordion menu that toggles the visibility of the element following a clicked item.

To search for an ancestor element instead of a sibling, use element.closest(). To search among descendants, use element.querySelector().

Browser Compatibility

Chrome Chrome
49+
1 or earlier ×
Firefox Firefox
57+
2.5 or earlier ×
Safari Safari
18+
3.1 or earlier ×
Edge Edge
80+
11 or earlier ×
IE IE
11+
8 or earlier ×
Opera Opera
48+
9 or earlier ×
iOS Safari iOS Safari
18+
2 or earlier ×
Android Browser Android Browser
37+
4 or earlier ×
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .