← Back to Index

.next()

Get the immediately following sibling of each element in the set of matched elements.

Examples

1. Get next sibling

// <h1>Title</h1><p>Text</p>
$('h1').next().css('color', 'red');

Result

<h1>Title</h1>
<p style="color: red;">Text</p>

2. Filter next sibling

// <li>A</li><li class="selected">B</li>
$('li').next('.selected').addClass('highlight');

Result

<li>A</li>
<li class="selected highlight">B</li>

Parameters

Parameter Type Description
selector string Optional. If specified, retrieves the next sibling only if it matches this selector.

Returns: A new SR object.