jQuery — Filtering
filter()
$("li").filter(".active");
$("li").filter(function(index, element) {
return $(element).text().length > 5;
});
not()
$("li").not(".inactive");
$("li").not(":first");
first(), last(), eq()
$("li").first();
$("li").last();
$("li").eq(2); // Third element (0-indexed)
has()
$("div").has("p");
$("div").has("img.highlight");
is()
if ($("li").first().is(".active")) {
console.log("Active");
}
slice()
$("li").slice(1, 4); // Elements 1, 2, 3
$("li").slice(2); // Elements from index 2
each()
$("li").each(function(index) {
$(this).addClass("item-" + index);
});
map()
var urls = $("a").map(function() {
return $(this).attr("href");
}).get();
Practical: Filtering List
$("#search").on("keyup", function() {
var term = $(this).val().toLowerCase();
$("ul li").each(function() {
var match = $(this).text().toLowerCase().indexOf(term) > -1;
$(this).toggle(match);
});
});
Mini Practice
- Filter elements by class
- Use a custom filter function
- Slice a list to show items 2-4
- Build a search filter
Up Next
Continue with AJAX — loading data from the server.
Related Topics
Frequently Asked Questions about Filtering
What is Filtering in jQuery?
Filtering is a fundamental concept in jQuery. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Filtering?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Filtering.
Why is Filtering important in jQuery?
Filtering is essential for jQuery development. Understanding this concept will help you write better code and solve real-world problems more effectively.