Filter Selectors

Loading concept...

jQuery Filter Selectors: The Magic Sorting Hat! 🎩

Imagine you have a big box of toys. You want to pick only certain toys—maybe the first one, or only the hidden ones, or toys that have a ball inside. jQuery filter selectors are like magic spells that help you pick exactly the toys you want!


🎯 What Are Filter Selectors?

Regular selectors find things by their name (like finding all <p> tags). Filter selectors go further—they filter those results based on position, visibility, or content.

Think of it like this:

  • Regular selector: “Give me all the books”
  • Filter selector: “Give me the first book” or “Give me books with pictures inside

📍 Position Filter Selectors

These pick elements based on where they sit in a line.

:first and :last

Like picking the first kid or last kid in a queue.

$("li:first")   // First <li> in the page
$("li:last")    // Last <li> in the page

:even and :odd

Like picking every other person in line (starting from 0).

$("tr:even")    // Rows 0, 2, 4...
$("tr:odd")     // Rows 1, 3, 5...

Real Example: Color every other row in a table:

$("tr:even").css("background", "gray");
$("tr:odd").css("background", "white");

:eq(index)

Pick the element at an exact position (starts at 0).

$("li:eq(2)")   // 3rd item (index 2)

:lt(index) and :gt(index)

  • :lt(n) = “less than” position n
  • :gt(n) = “greater than” position n
$("li:lt(3)")   // Items 0, 1, 2
$("li:gt(1)")   // Items 2, 3, 4...

👶 Child Position Selectors

These work inside each parent separately. Like checking who’s first in each classroom, not the whole school.

:first-child and :last-child

$("li:first-child")  // First <li> in EACH <ul>
$("li:last-child")   // Last <li> in EACH <ul>

:nth-child(n)

Pick the nth child (starts at 1, not 0!).

$("li:nth-child(2)")     // 2nd child in each list
$("li:nth-child(odd)")   // 1st, 3rd, 5th...
$("li:nth-child(even)")  // 2nd, 4th, 6th...
$("li:nth-child(3n)")    // Every 3rd child

:only-child

Picks elements that are the only child of their parent.

$("p:only-child")  // <p> with no siblings
graph TD A["Parent"] --> B["Child 1"] A --> C["Child 2"] A --> D["Child 3"] B -->|:first-child| E["✓ Selected"] D -->|:last-child| F["✓ Selected"] C -->|:nth-child 2| G["✓ Selected"]

👁️ Visibility Selectors

These pick elements based on whether you can see them or not.

:visible

Picks elements you can see on the page.

$("div:visible")  // All visible divs

:hidden

Picks elements that are hidden (display:none, visibility:hidden, or type=“hidden”).

$("input:hidden")   // Hidden input fields
$("div:hidden")     // Hidden divs

Helpful Tip: Hidden elements include:

  • Elements with display: none
  • Elements with visibility: hidden
  • Form inputs with type="hidden"
  • Elements with zero width and height

📝 Content Selectors

These pick elements based on what’s inside them.

:contains(text)

Finds elements containing specific text.

$("p:contains('hello')")  // <p> with "hello" inside

Note: This is case-sensitive! “Hello” ≠ “hello”

:empty

Picks elements with nothing inside (no text, no children).

$("td:empty")  // Empty table cells

:parent

Opposite of :empty—picks elements that have something inside.

$("td:parent")  // Table cells with content
graph TD A["td element"] --> B{Has content?} B -->|Yes| C[":parent ✓"] B -->|No| D[":empty ✓"]

🚫 The :not() Selector

The “everything except” selector. Like saying “Give me all cookies EXCEPT chocolate ones.”

$("input:not(:checked)")     // Unchecked inputs
$("li:not(.active)")         // li without .active class
$("div:not(:hidden)")        // Visible divs
$("p:not(:first)")           // All p except the first

Powerful Combos:

// All inputs except submit buttons
$("input:not([type='submit'])")

// All list items except first and last
$("li:not(:first):not(:last)")

🔍 The :has() Selector

Finds parents that contain a specific child. Like asking “Which boxes have a red ball inside?”

$("div:has(p)")        // Divs containing a <p>
$("ul:has(.active)")   // Lists with an .active item
$("tr:has(input)")     // Table rows with inputs

Real Example:

// Highlight rows that have checkboxes
$("tr:has(input[type='checkbox'])")
  .css("background", "yellow");
graph TD A["div"] --> B["p"] A --> C["span"] D["div"] --> E["span"] A -->|:has p| F["✓ Selected"] D -->|:has p| G["✗ Not Selected"]

🎮 Quick Reference Table

Selector What It Does Example
:first First element overall $("li:first")
:last Last element overall $("li:last")
:even Even indexes (0,2,4) $("tr:even")
:odd Odd indexes (1,3,5) $("tr:odd")
:eq(n) Element at index n $("li:eq(2)")
:lt(n) Before index n $("li:lt(3)")
:gt(n) After index n $("li:gt(1)")
:first-child First child per parent $("li:first-child")
:last-child Last child per parent $("li:last-child")
:nth-child(n) Nth child (starts at 1) $("li:nth-child(2)")
:only-child Only child of parent $("p:only-child")
:visible Visible elements $("div:visible")
:hidden Hidden elements $("input:hidden")
:contains(text) Has text inside $(":contains('hi')")
:empty No content inside $("td:empty")
:parent Has content inside $("td:parent")
:not(sel) Exclude matches $("li:not(.done)")
:has(sel) Contains child $("div:has(img)")

💡 Key Differences to Remember

:first vs :first-child

  • :first → ONE element from the whole page
  • :first-child → First child in EACH parent

:eq() vs :nth-child()

  • :eq(n) → Counts from 0, works globally
  • :nth-child(n) → Counts from 1, works per parent

:not() vs :has()

  • :not() → Exclude elements matching a selector
  • :has() → Include parents containing a selector

🚀 You Did It!

Filter selectors are your precision tools. They help you pick exactly what you need from a page. Start with the simple ones like :first and :hidden, then explore :not() and :has() for powerful combinations!

Remember: Just like a sorting hat picks the right house, filter selectors pick the right elements! 🎩✨

Loading story...

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Interactive content is coming soon.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Cheatsheet is coming soon.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Quiz is coming soon.

Flashcard Preview

Flashcard - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Flashcards are coming soon.