.next()S2C Home « Traversal « .next()

Next sibling element retrieval.

Description

The .next() method is used to retrieve the immediately following sibling of each element within the matched set.

Syntax

Signature Description
.next()Retrieve the immediately following sibling of each element within the matched set.
.next( selector )Retrieve the immediately following sibling of each element within the matched set, but only if it matches the specified selector.

Parameters

Parameter Description Type
selectorA string containing a CSS or custom jQuery selector.Selector

Return

A jQuery object either containing the elements matched or empty.

.next() ExampleTop

Retrieve the immediately following sibling of each element within the matched set.

In the example below we select all immediately following sibling elements of 'p' elements within the '#main' section of the page and turn their background colour yellow.


$(function(){
  $('#btn3').on('click', function() {
    $('#main p').next()
                .css('backgroundColor', 'yellow');
  });
});

Press the button below to action the above code:

.next( selector ) ExampleTop

Retrieve the immediately following sibling of each element within the matched set, but only if it matches the specified selector.

In the example below we select all immediately following 'h4' elements of 'form' elements within the '#main' section of the page and turn their background colour orange.


$(function(){
  $('#btn4').on('click', function() {
    $('#main form').next('h4')
                   .css('backgroundColor', 'orange');
  });
});

Press the button below to action the above code: