Basics Lesson 3 of 10
jQuery Selectors and Filters
Select elements with jQuery using CSS selectors, attribute selectors, jQuery filters like :first, :even, :visible and :checked, and methods like .eq, .filter and .not.
jQuery uses the same selectors as CSS, plus some extras. If you can style it with CSS, you can select it with jQuery.
Basic selectors
| Selector | Selects |
|---|---|
$("p") | All <p> elements |
$("#total") | The element with id total |
$(".card") | All elements with class card |
$("ul li") | All <li> inside a <ul> |
$("h2, h3") | All h2 and h3 elements |
$("input[type='email']") | Email inputs |
$("a[href^='https']") | Links starting with https |
<script src="/vendor/jquery.min.js"></script>
<ul id="grades">
<li class="pass">Math – 88</li>
<li class="fail">Science – 72</li>
<li class="pass">English – 91</li>
<li class="fail">Filipino – 70</li>
</ul>
<button id="b1">Highlight failing</button>
<button id="b2">Bold every item</button>
<script>
$("#b1").on("click", function () { $(".fail").css("background", "#fbe3e0"); });
$("#b2").on("click", function () { $("#grades li").css("font-weight", "bold"); });
</script>
jQuery filters
<script src="/vendor/jquery.min.js"></script>
<table border="1" cellpadding="6" id="t">
<tr><th>Name</th><th>Section</th></tr>
<tr><td>Ana</td><td>Rizal</td></tr>
<tr><td>Ben</td><td>Mabini</td></tr>
<tr><td>Carla</td><td>Rizal</td></tr>
<tr><td>Dan</td><td>Luna</td></tr>
</table>
<p><label><input type="checkbox" checked> Math</label> <label><input type="checkbox"> Art</label> <label><input type="checkbox" checked> ICT</label></p>
<p id="out"></p>
<script>
$("#t tr:even").css("background", "#f1f5f3"); // zebra rows
$("#t tr:first").css("background", "#0f5c4d").css("color", "white");
$("#t td:contains('Rizal')").css("color", "#0038A8");
$("#out").text("Checked: " + $("input:checked").length + " subject(s)");
</script>
| Filter | Selects |
|---|---|
:first, :last | First / last match |
:even, :odd | Even / odd positions (counting from 0) |
:contains('text') | Elements containing the text |
:checked, :selected, :disabled | Form states |
:visible, :hidden | Shown / hidden elements |
Filters like :first, :even and :contains are jQuery extras; plain CSS doesn't have them. In modern jQuery, the methods below are preferred because they're faster.
Filtering methods
$("li").first(); // first li
$("li").last(); // last li
$("li").eq(2); // the li at index 2
$("li").filter(".pass"); // only those with class pass
$("li").not(".pass"); // all except .pass
$("li").length; // how many were found
this
Inside an event handler, $(this) is the element that triggered the event:
<script src="/vendor/jquery.min.js"></script>
<button class="seat">A1</button> <button class="seat">A2</button> <button class="seat">A3</button>
<p id="pick">Pick a seat.</p>
<script>
$(".seat").on("click", function () {
$(".seat").css("background", "");
$(this).css("background", "gold");
$("#pick").text("You picked seat " + $(this).text());
});
</script>
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04