.next() JQ 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() Example Traversal  <<  Top

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 .content section of the page and turn their background colour yellow.


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

Press the button below to action the above code:

.next( selector ) Example Traversal  <<  Top

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 'p' elements of 'h3' elements within the .content section of the page and turn their background colour orange.


$(function(){
  $('#btn4').on('click', function() {
    $('.content h3').next('p')
                .css('backgroundColor', 'orange');
  });
});

Press the button below to action the above code:

Related Tutorials

jQuery Basic Tutorials - Lesson 6 - Tree Traversal