Selecting, Traversing, and Filtering - Appendices - Web Development with jQuery (2015)

Web Development with jQuery (2015)

Part IV. Appendices

Appendix C. Selecting, Traversing, and Filtering

Method/Property

Description

Return Value

Selecting

$(selector)

Makes a selection from the document.

jQuery

jQuery(selector)

An alternative name for the preceding dollar sign method.

jQuery

length

The number of selected elements.

Number

get()

Returns all selected elements as an array, rather than as a jQuery object.

Array

get(index)

Returns a single element from the selection; the index argument is the element's position in the selection, offset from zero.

Element

index(subject)

Searches the selection for the specified element and returns that element's position in the selection offset from zero.

Number

Traversing and Filtering

add(selector)

Adds one or more elements to the selection by virtue of an additional selector.

jQuery

add(elements)

Adds one or more elements to the selection by virtue of one or more element object references.

jQuery

add(html)

Adds one or more elements to the selection by virtue of an HTML fragment string that is parsed and converted into DOM element object references.

jQuery

add(selection)

Adds one or more elements to the selection by virtue of an existing selection reference.

jQuery

add(selector, context)

Adds one or more elements to the selection by virtue of a selector. A context provides the relative point in the document where the selector should be carried out.

jQuery

addBack([selector])

Adds a set or selection of elements to the current selection; this can optionally be filtered by a selection.

jQuery

andSelf()

Adds the previous selection to the current selection. Deprecated in jQuery 1.8.

jQuery

children([selector])

Makes a selection within the context of the matched elements' children. The selector argument is optional; to select all children of all the selected elements, simply omit the selector argument.

jQuery

closest(selector[, context])

Similar to the parents() method, except this method begins with the element itself, rather than its parent, and either matches the element itself or travels up the DOM to find the right ancestor.
If the optional context argument is provided, it provides a DOM element within which a matching element can be found.

jQuery

closest(selection)

The closest() method may also use an existing selection reference.

jQuery

closest(element)

The closest() method may also use a DOM element object reference directly.

jQuery

contents()

Gets children elements of each matched element, including text and comment nodes, which are normally excluded from jQuery method operations.

jQuery

each(function(key, value))

Executes a callback function for every element in a selection.
As with most jQuery callback functions, this refers to the current element within the callback function, and the callback function is provided the argument list: offset, element.
Returning true from the callback function provides a result similar to a continue statement. Returning false provides a result similar to a break statement.

jQuery

$(Array).each(
function(key, value)
)
$.each(Array, function)
$.each(Object, function)

Executes a callback function for every element in an array.
As with most jQuery callback functions, this refers to the current item within the callback function, and the callback function is provided the argument list: key, value.
Returning true from the callback function provides a result similar to a continue statement. Returning false provides a result similar to a break statement.

Object

end()

Ceases any filtering that took place and returns the current selection to its previous state.

jQuery

eq(index)

Reduces a selection to a single element, where index is the number representing the element's position in the selection offset from zero.

jQuery

eq(-index)

Reduces a selection to a single element, where index is a negative number representing the element's position from the last element in the selection.

jQuery

filter(selector)

Removes all elements that do not match the specified selector.

jQuery

filter(function(index))

The filter() method may alternatively accept a function as its first argument, which works identically to the jQuery $.each() method. The function is executed for each item selected.
The function must return a boolean value, where true indicates that the element should remain in the result set, and false indicates that the element should be removed from the result set.
As with most jQuery callback functions, this refers to the current element within the callback function, and the callback function is provided the argument list: offset, element.

jQuery

filter(element)

Filters the selection based on the JavaScript node passed in the element argument, elements that match the node remain in the selection; elements that do not match the node are dropped from the selection. One or more DOM element object references can be passed in.

jQuery

filter(selection)

Filters the selection based on the jQuery object passed in the selection object (the result of a jQuery selection) argument. If elements in the selection match the jQuery object, they remain in the selection; if not, those elements are removed from the selection.

jQuery

find(selector)

Makes a selection within the context of matched elements' descendants.

jQuery

find(selection)

Finds element descendants using an existing selection object to match element descendants against.

jQuery

find(element)

Finds element descendants using an existing DOM element object to match descendants against.

jQuery

first()

Removes all elements from the selection, except the first.

jQuery

has(selector)

Reduces a previous selection based on whether the selection matches the selector provided in the selector argument.

jQuery

has(element)

Reduces a selection based on whether the selection matches the DOM element provided in the element argument.

jQuery

is(selector)

Returns true if one or more elements match the condition specified in the selector. For example:
$('input').is(':checked')

Boolean

is(function(index))

Returns true if the callback function returns true for one or more elements. Returns false if the callback function returns false for every item passed to the callback function.
As with most jQuery callback functions, this refers to the current element within the callback function, and the callback function is provided the argument list: offset, element.

Boolean

is(selection)

Returns true if the selection matches one or more items present using an existing jQuery selection.

Boolean

is(elements)

Returns true if the selection matches any of the DOM element object reference(s) provided.

Boolean

last()

Removes all elements from the selection, except the last.

jQuery

map(function(index, element))

Like each(), each matched element is passed into a callback function. The return value of each callback function is used to build a new jQuery object, creating a mapping to a new array of element references. Returning the item or array of items includes it in the new array. Returning null or undefined results in no item added to the new array.
As with most jQuery callback functions, this refers to the current element within the callback function, and the callback function is provided the argument list: offset, element.
Returning true from the callback function provides a result similar to a continue statement. Returning false provides a result similar to a break statement.

jQuery

$(Array).map(
function(key, value)
)

Like each(), each array item is passed into a callback function. The return value of each callback function is used to build a new array, creating a mapping to a new array. Returning the item or array of items includes it in the new array. Returning null or undefined results in no item added to the new array.
As with most jQuery callback functions, this refers to the current item within the callback function, and the callback function is provided the argument list: key, value.
Returning true from the callback function provides a result similar to a continue statement. Returning false provides a result similar to a break statement.

Array

next([selector])

Selects the next sibling element; the selector argument is optional.

jQuery

nextAll([selector])

Selects all subsequent sibling elements; the selector argument is optional.

jQuery

nextUntil([selector][, filter])

Selects all subsequent sibling elements up to but not including the element matched by the selector.
If the optional filter argument is specified, the matched elements are further filtered against the selector you provide to the filter argument.

jQuery

nextUntil([element][, filter])

Selects all subsequent sibling elements up to but not including the element object reference provided.
If the optional filter argument is specified, the matched elements are further filtered against the selector you provide to the filter argument.

jQuery

not(selector)

Removes elements from the selection that match the specified selector.

jQuery

not(elements)

Removes elements from the selection that match the specified DOM element object reference(s).

jQuery

not(function(index))

Removes elements from the selection based on the whether the callback function returns true or false. If the callback function returns true, the element current is excluded from the selection; if the callback function returns false, it is included in the selection.
As with most jQuery callback functions, this refers to the current element within the callback function, and the callback function is provided the argument list: offset, element.

jQuery

not(selection)

Removes elements from the selection based on whether the elements in the selection match elements in the provided selection object.

jQuery

offsetParent()

Gets the closest ancestor element that is positioned with position absolute, relative or fixed.

jQuery

parent([selector])

Selects all immediate parent elements; the selector argument is optional.

jQuery

parents([selector])

Selects all ancestor elements; the selector argument is optional.

jQuery

parentsUntil([selector][, filter])

Matches parent or ancestor elements up to but not including the element that matches the selector.
If the optional filter argument is specified, the matched elements are further filtered via the selector that you provide in the filter argument.

jQuery

parentsUntil([element][, filter])

Matches parent or ancestor elements up to but not including the element that matches the provided DOM element object reference.
If the optional filter argument is specified, the matched elements are further filtered via the selector that you provide in the filter argument.

jQuery

prev([selector])

Selects the previous sibling element; the selector argument is optional.

jQuery

prevAll([selector])

Selects all preceding sibling elements; the selector argument is optional.

jQuery

prevUntil([selector][, filter])

Selects all preceding sibling elements up to but not including the element matched by the selector.
If the optional filter argument is specified, the matched elements are further filtered against the selector you provide to the filter argument.

jQuery

prevUntil([element][, filter])

Selects all preceding sibling elements up to but not including the element object reference provided.
If the optional filter argument is specified, the matched elements are further filtered against the selector you provide to the filter argument.

jQuery

siblings([selector])

Selects all sibling elements; the selector argument is optional.

jQuery

slice(start[, end])

Selects a subset of the selection, where each index is a number representing the element's position in the selection offset from zero.

jQuery