Filtering

jQuery provides methods for filtering the current selection.

method description
filter(sel | cb)

Creates a new jQuery object by filtering the collection using a given selector or callback function.

The sel parameter can be

  • string with css selector
  • DOM element or array of DOM elements
  • an existing jQuery object

The cb callback must return a boolean value. It has two parameters:

  • index - zero-based index of current element
  • element - the current DOM element, you can also use the this keyword to access the current element
not(sel | cb)

Creates a new jQuery object by removing the specified elements using a given selector or callback function.

The sel parameter can be

  • string with css selector
  • DOM element or array of DOM elements
  • an existing jQuery object

The cb callback must return a boolean value. It has two parameters:

  • index - zero-based index of current element
  • element - the current DOM element, you can also use the this keyword to access the current element
even() Creates a new jQuery object from the even elements. Counting starts from zero.
odd() Creates a new jQuery object from the odd elements. Counting starts from zero.
end()

Ends the most recent filtering operation in the current chain and returns previous state.

Some methods create a new jQuery object based on the current jQuery object. When this happens, a new object will be pushed onto the stack, which is stored inside the object. If you want to revert to the previous selection, call this method.