back to Overview

.next()

Finds the next sibling elements matching the given selector or filter function for each list element, and returns the results as a list.
Web module only.

Syntax Variants

list.next()
list.next(selector)
list.next(maxDepth)
list.next(selector, maxDepth)
list.next(filterFunc)
list.next(filterFunc, maxDepth)

Parameters

selector (optional)
any selector valid for $(), including CSS selectors and lists.
Selectors are optimized for '*', '.classname', 'tagname' and 'tagname.classname'. The performance for other selectors is relative to the number of matches for the selector in the document. Default is '*', which includes all elements.
filterFunc
a function(node) returning true for those nodes that match.
maxSiblings (optional)
the maximum number of siblings to include per list element. Defaults to 1.
(return value)
the new list that contains matching siblings elements. Duplicate nodes will be automatically removed.

Description

Finds the next sibling elements matching the given selector or filter function for each list element, and returns the results as a list. By default, only one match is returned per list element, but you can increase the number of results using the second argument. You can get an infinite number of results per list element by passing -1.

next(selector, maxDepth) is just a shortcut for trav('nextSibling', selector, maxDepth||1). next() uses trav() to traverse the DOM tree using nextSibling for each list element, until it either finds a matching element the specified amount of matches or there are no more elements. All matches will added to the result list. The result list is filtered to include only unique elements.

Instead of the selector, you can also specify a function that evaluates whether an element matches.

Example

Returns the immediate sibling element of a node:

var parent = $('#child').next();

Example

Returns the next <hr> element following the node:

var parent = $('#child').next('hr');

Example

Returns all <hr> elements following the node:

var parent = $('#child').next('hr', -1);

Example

Returns the next 2 sibling elements of a node:

var parent = $('#child').next(2);

Example

Returns all following siblings of a node:

var parent = $('#child').next(-1);

Example

Returns a list of all direct sibling elements that have a class that starts with 'special':

var specialSiblings = $('.myElements').next(function(node) {
    return /(^|\s)special/.test(node.className);
});

See also..

  • trav() allows you to select other relatives such as preceding siblings or children.

Comments

comments powered by Disqus

back to Overview

Functions