HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

jQuery Selectors

Selectors are the foundation of everything you do in jQuery. They allow you to "pick" or "select" the HTML elements you want to work with. If you are already familiar with CSS selectors, you are 90% of the way there because jQuery uses almost the exact same syntax.


The Power of $()

In jQuery, you use the $() function to select elements. Inside the parentheses, you place a string that tells jQuery what to look for. Once an element is selected, you can immediately chain an action to it.

Generic Pattern:
$( "your-selector-here" ).action();

1. Basic Selectors

These are the selectors you will use most often in your daily development.

Element Selector
$("p")

Selects all <p> elements on the page.

ID Selector
$("#header")

Selects the single element with id="header".

Class Selector
$(".btn")

Selects all elements with class="btn".


2. Attribute Selectors

You can target elements based on their HTML attributes, such as href, src, or type.

Selector Description
$("[href]") Selects all elements with an href attribute.
$("a[target='_blank']") Selects all links that open in a new window.
$("input[type='text']") Selects all text input fields in a form.

3. Advanced Pseudo-Selectors

jQuery provides several custom "pseudo-selectors" that make it incredibly easy to target specific elements in a list.

// Selects the first 

element $("p:first").css("color", "red"); // Selects the last

  • element $("li:last").addClass("active"); // Selects all even numbered table rows $("tr:even").css("background", "#eee"); // Selects all odd numbered list items $("li:odd").fadeOut();

  • Common jQuery Selectors Reference

    Here is a handy reference table of the most common selectors you'll need:

    Syntax Description
    $("*") Selects every single element on the page.
    $(this) Selects the current HTML element being interacted with.
    $("p.intro") Selects all <p> elements with class="intro".
    $("ul li:first") Selects the first <li> of the first <ul>.
    $(":button") Selects all <button> elements and input elements of type button.
    $("tr:even") Selects all even table rows (2nd, 4th, etc.).
    $("tr:odd") Selects all odd table rows (1st, 3rd, etc.).

    Real-World Demo: Interactive Selection

    Imagine you have a simple list. With one line of jQuery, we can target specific items based on their position.

    <ul id="demoList">
        <li>Item 1 (Odd)</li>
        <li>Item 2 (Even)</li>
        <li>Item 3 (Odd)</li>
        <li>Item 4 (Even)</li>
    </ul>
    Important Note: Unlike CSS where indices usually start at 1, jQuery's :even and :odd selectors are 0-indexed. This means the first item is 0 (even), the second is 1 (odd), and so on.

    Why Use jQuery Selectors Over Vanilla JS?

    Vanilla JavaScript

    Requires different methods for different targets:

    • getElementById()
    • getElementsByClassName()
    • querySelectorAll()
    jQuery Selection

    One function to rule them all:

    • $() handles everything.
    • Supports older browsers automatically.
    • Returns a jQuery object with built-in methods.

    Key Points to Remember

    • Use #id for a specific element (IDs should be unique).
    • Use .class for multiple elements with the same styling or behavior.
    • The $() selector returns a collections of elements even if only one is found.
    • You can "drill down" selectors just like CSS: $("div p span").
    • Selectors are much faster than selecting elements manually in pure JS for complex queries.