back to Overview

.only()

Returns a new list that contains only those elements that match the given selector, match the callback function or have the given index.
only(index) is always available. All others variants are only in the Web module.

Syntax Variants

list.only()
list.only(selector)
list.only(filterFunc)
list.only(index)

Parameters

selector
any selector valid for $(), including CSS selectors and lists.
Selectors are optimized for '*', '.classname', 'tagname' and 'tagname.classname'. The performance for other selectors depends on the number of matches for the selector in the document. Default is '*', which keeps all elements (but no other nodes such as text nodes).
filterFunc
a function(node) returning true for those nodes that are approved.
index
the index of the element to keep. All elements with other index will be omitted. If there is no element with this index in the list, the returned list is empty.
(return value)
a new list containing only elements matched by the selector/function/index.

Description

Returns a new list that contains only those elements that match the given selector, match the callback function or have the given index. If no parameter has been given, the method keeps all HTML elements and removes everything else (same as '*').

When you use selectors, please note that this method is optimized for the four simple selector forms '*', '.classname', 'tagname' and 'tagname.classname'. If you use any other kind of selector, be aware that selectors that match many elements can be slow.

Example

Returns only those list elements have the classes 'listItem' and 'myClass':

var myLis = $('li.listItem').only('.myClass');

Example

Returns a list of all forms:

var forms = $('#content *').only('form');

See also..

  • not() creates a list of all elements not matching the selector.
  • select() executes a selector on the descendants of the list elements.
  • filter() offers function-based filtering.

Comments

comments powered by Disqus

back to Overview

Functions