jQuery nextAll() Method

The jQuery nextAll() method can be used to get all the next sibling elements of the selected element. It returns all the elements next sibling of the specified elements.

jQuery nextAll() Method

Syntax

The syntax of this method is given below:-

Syntax
$(selector).nextAll(filterElement)

Description of the Parameters

The description of these parameters is given below.

Parameter Name Description
selector Specify the element to select and get all the next sibling elements. You can use the element tag name, class name, or id to select. It is a required field.
filterElement It is the element to specify and find all next matching sibling search. It is an optional field.

jQuery nextAll() Method to Return All Next Sibling Element

If you want to return all the next sibling elements, you have to use the method as given below. It selects all the elements next to the selected element. The below example contains the list elements and the button element.

Example

Test it Live

Output

  • First list element.
  • Second list element.
  • Middle list element.
  • Forth list element.
  • Last list element.


When you click the button given above, it returns all the elements next to the list element with class ‘.middle’. There are two elements it selects and applies the color and border to it.

Get All Matching Next Sibling Element

You can also pass the tag name or class name of the element as the parameter to get all next elements. There are two list elements with the same class name after the first list item.

Example

Test it Live

Output

  • 1st li item
  • 2nd li item
  • 3rd li item
  • Last li item


Click the button given above to get all the specified matching next sibling element. It applies the CSS to the elements that are selected using the jQuery nextAll() method.

You may also like to read

I hope you like this tutorial on jQuery nextAll() method. If you have any queries regarding the tutorial, please comment below.

References