Traversing Sibling Elements in jQuery

In jQuery, sibling traversal allows you to navigate sideways through the DOM tree — selecting elements that share the same parent. This is useful when you need to manipulate adjacent content or loop through elements at the same hierarchical level. jQuery provides several methods to access siblings in both forward and backward directions, with optional filters to narrow your selection.

jQuery Sibling Traversal Methods

Whether you're selecting the next element, all following siblings, or everything until a certain match, jQuery offers flexible traversal methods:

  • siblings([selector]): Gets all siblings (excluding the original element).
  • next([selector]): Selects the immediately next sibling element.
  • nextAll([selector]): Selects all next sibling elements.
  • nextUntil(selector, [filter]): Selects all next siblings until a selector is matched.
  • prev([selector]): Selects the immediately previous sibling element.
  • prevAll([selector]): Selects all previous sibling elements.
  • prevUntil(selector, [filter]): Selects all previous siblings until a selector is matched.

Get All Siblings Using siblings()

The siblings() method selects all sibling elements that share the same parent, excluding the original element itself.

siblings() Example 1

$(function () {
	$('.item').on('click', function () {
		// Reset All
		$('.item').removeClass('highlight');
		// Highlight Siblings
		$(this).siblings().addClass('highlight');
	});
});

Key Points

  • Excludes the clicked element itself.
  • Supports optional selector to limit matched siblings.
  • Useful for highlighting surrounding elements of the same group.

Optional Selector

Click an item to highlight its sibling elements that have the .special class using siblings('.special') method.

siblings() Example 2

siblings() Method With Optional Selector
$(function () {
	$('.item').on('click', function () {
		$('.item').removeClass('highlight');
		$(this).siblings('.special').addClass('highlight');
	});
});

Navigating Forward in DOM Tree

next() Method

The next() method selects only the immediate next sibling.

next() Example

$(function () {
	$('.box').on('click', function () {
		$('.box').removeClass('highlight');
		// Selects Next sibling element
		$(this).next().addClass('highlight');
	});
});

nextAll() Method

The nextAll() method selects all siblings after the current element.

nextAll() Example

$(function () {
	$('.box').on('click', function () {
		$('.box').removeClass('highlight');
		// Selects all next siblings
		$(this).nextAll().addClass('highlight'); 
	});
});

nextUntil() Method

The nextUntil() method selects siblings up to but excluding the matched selector.

nextUntil() Example

$(function () {
	$('.box').on('click', function () {
		$('.box').removeClass('highlight');
		// Selects siblings until STOP
		$(this).nextUntil('.stop-here').addClass('highlight');
	});
});

Forward Traversal Notes:

  • next() = one step forward.
  • nextAll() = all forward siblings.
  • nextUntil() = forward traversal with a stopping point.

Navigating Backward in DOM Tree

prev() Method

The prev() method selects the immediate previous sibling.

prev() Example

$(function () {
	$('.item').on('click', function () {
		$('.item').removeClass('highlight');
		// Selects previous sibling
		$(this).prev().addClass('highlight');
	});
});

prevAll() Method

Selects all previous siblings.

prevAll() Example

$(function () {
	$('.step').on('click', function () {
		$('.step').removeClass('highlight');
		// Selects all previous siblings
		$(this).prevAll().addClass('highlight');
	});
});

prevUntil() Method

Selects all previous siblings up to but excluding the matched selector.

prevUntil() Example

$(function () {
	$('.item').on('click', function () {
		$('.item').removeClass('highlight');
		// Selects previous siblings until 'STOP' selector
		$(this).prevUntil('.stop').addClass('highlight');
	});
});

Backward Traversal Notes:

  • prev() = one step back.
  • prevAll() = all previous siblings.
  • prevUntil() = select backward until a stop condition.

Pro Tip:

All jQuery sibling traversal methods exclude the starting element itself, and *Until() methods also exclude the ending selector from the result.