jQuery Filter Selectors

jQuery filter selectors allow you to refine your selection by filtering elements based on certain criteria. These selectors are powerful for narrowing down elements to apply specific actions. Here’s a detailed guide to commonly used jQuery filter selectors:

Basic Filter Selectors

  1. $("*")
    Selects all elements in the DOM.

    $("*").css("border", "1px solid red");
    

    Try It Now

  2. $("element:first")
    Selects the first occurrence of the specified element.

    $("p:first").css("color", "blue");
    

    Try It Now

  3. $("element:last")
    Selects the last occurrence of the specified element.

    $("p:last").css("color", "green");
    

    Try It Now

  4. $("element:even")
    Selects all elements with an even index (starting from 0).

    $("li:even").css("background-color", "lightgrey");
    

    Try It Now

  5. $("element:odd")
    Selects all elements with an odd index.

    $("li:odd").css("background-color", "lightblue");
    

    Try It Now

  6. $("element:eq(index)")
    Selects the element with the specified index.

    $("li:eq(2)").css("font-weight", "bold");
    

    Try It Now

  7. $("element:gt(index)")
    Selects all elements with an index greater than the specified index.

    $("li:gt(2)").css("text-decoration", "underline");
    

    Try It Now

  8. $("element:lt(index)")
    Selects all elements with an index less than the specified index.

    $("li:lt(2)").css("color", "red");
    

    Try It Now

Content-Based Filter Selectors

  1. $(":contains('text')")
    Selects elements that contain the specified text.

    $("p:contains('jQuery')").css("font-style", "italic");
    

    Try It Now

  2. $(":has(selector)")
    Selects elements that contain at least one element matching the selector.

    $("div:has(p)").css("border", "1px solid black");
    

    Try It Now

  3. $(":empty")
    Selects elements that have no children (including text nodes).

    $("div:empty").css("background-color", "yellow");
    

    Try It Now

  4. $(":parent")
    Selects elements that are parents (contain child elements).

    $("div:parent").css("padding", "10px");
    

    Try It Now

Visibility Filter Selectors

  1. $(":visible")
    Selects all elements that are visible.

    $("div:visible").css("border", "2px solid green");
    

    Try It Now

  2. $(":hidden")
    Selects all elements that are hidden.

    $("div:hidden").show();
    

    Try It Now

Attribute-Based Filter Selectors

  1. $("[attribute]")
    Selects elements with the specified attribute.

    $("[title]").css("color", "purple");
    

    Try It Now

  2. $("[attribute='value']")
    Selects elements with the specified attribute and value.

    $("[type='text']").css("border", "1px solid blue");
    

    Try It Now

  3. $("[attribute^='value']")
    Selects elements with an attribute value starting with the specified value.

    $("[href^='http']").css("color", "orange");
    

    Try It Now

  4. $("[attribute$='value']")
    Selects elements with an attribute value ending with the specified value.

    $("[src$='.jpg']").css("border", "1px solid red");
    

    Try It Now

  5. $("[attribute*='value']")
    Selects elements with an attribute value containing the specified value.

    $("[class*='intro']").css("font-weight", "bold");
    

    Try It Now

Form Filter Selectors

  1. $(":input")
    Selects all input, textarea, select, and button elements.

    $(":input").css("border", "1px solid grey");
    

    Try It Now

  2. $(":checked")
    Selects all checked checkboxes or radio buttons.

    $(":checked").css("background-color", "lightgreen");
    

    Try It Now

  3. $(":selected")
    Selects all selected options in a dropdown list.

    $(":selected").css("font-weight", "bold");
    

    Try It Now

Custom Filter Functions

  1. $().filter(function)
    Filters the set of matched elements by applying a custom function.

    $("li").filter(function(index) {
        return index % 2 === 0;
    }).css("background-color", "lightgrey");
    

    Try It Now

By using these jQuery filter selectors, you can efficiently target specific elements in your document for dynamic manipulation, styling, or event handling.