
Filters in Jquery
In jQuery, filters are used to narrow down a set of selected elements based on specific criteria. Filters can be broadly categorized into basic filters, content filters, visibility filters, attribute filters, child filters, and form filters.
Here’s a complete breakdown of the filters available in jQuery:
? 1. Basic Filters
These are used to filter based on position or basic characteristics.
Selector | Description |
---|---|
:first | Selects the first element |
:last | Selects the last element |
:even | Selects elements with even index (0-based) |
:odd | Selects elements with odd index |
:eq(index) | Selects an element with a specific index |
:gt(index) | Selects elements with index greater than |
:lt(index) | Selects elements with index less than |
:not(selector) | Excludes elements that match the selector |
Example:
$(<ul> "li:even").css("background-color", "lightgray"); // Even index items $("li.special").filter(":contains('Item 3')").css("color", "blue");</script>
? Conclusion:
jQuery filters help you quickly select and manipulate elements based on conditions like position, content, visibility, attributes, and relationships. They're essential for writing clean and efficient jQuery code.