Filtering Selector Results Using a Filter Function

$("option").filter(  function (index) {   return (this.value>5); });   //selects only the <option> elements with value=5 $("div").filter(".myClass");   //selects only the <div> elements with class="myClass"

The .filter(filter) method reduces the set to only those items matching the specified filter. The filter can be a selector, a specific DOM element, a jQuery object, or a function that tests each element and returns true if it should be included.

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.