Top 50 jQuery Selectors

Sam Deering
Share

We all pretty much use jQuery selectors in all the code we write, it”s the most powerful way to specify what elements we want from the DOM. This list is to server as a great reference material for every jQuery developer out there. Note that they are not in any particular order.

  1. $(“*”) – This selector selects all elements in the document.
  2. $(“p > *”) – This selector selects all elements that are children of a paragraph element.
  3. $(“#specialID”) – This selector function gets the element with id=”specialID”.
  4. $(“.specialClass”) – This selector gets all the elements that have the class of specialClass.
  5. $(“li:not(.myclass)”) – Selects all elements matched by
  6. that do not have.
  7. $(“a#specialID.specialClass”) – This selector matches links with an id of specialID and a class of specialClass.
  8. $(“p a.specialClass”) – This selector matches links with a class of specialClass declared within

    elements.

  9. $(“ul li:first”) – This selector gets only the first
  10. element of the
      .
  11. $(“#container p”) – Selects all elements matched by

    that are descendants of an element that has an id of container.

  12. $(“li > ul”) – Selects all elements matched by
      that are children of an element matched by

    • $(“strong + em”) – Selects all elements matched by that immediately follow a sibling element matched by .
    • $(“p ~ ul”) – Selects all elements matched by
        that follow a sibling element matched by

        .

    • $(“code, em, strong”) – Selects all elements matched by or or .
    • $(“p strong, .myclass”) – Selects all elements matched by that are descendants of an element matched by

      as well as all elements that have a class of myclass.

    • $(“:empty”) – Selects all elements that have no children.
    • $(“p:empty”) – Selects all elements matched by

      that have no children.

    • $(“div[p]”) – Selects all elements matched by
      that contain an element matched by

      .

    • $(“p[.myclass]”) – Selects all elements matched by

      that contain an element with a class of myclass.

    • $(“a[@rel]”) – Selects all elements matched by that have a rel attribute.
    • $(“input[@name=myname]”) – Selects all elements matched by that have a name value exactly equal to myname.
    • $(“input[@name^=myname]”) – Selects all elements matched by that have a name value beginning with myname.
    • $(“a[@rel$=self]”) – Selects all elements matched by

      that have a class value ending with bar

    • $(“a[@href*=domain.com]”) – Selects all elements matched by that have an href value containing domain.com.
    • $(“li:even”) – Selects all elements matched by
    • that have an even index value.
    • $(“tr:odd”) – Selects all elements matched by
      that have an odd index value.
    • $(“li:first”) – Selects the first
    • element.
    • $(“li:last”) – Selects the last
    • element.
    • $(“li:visible”) – Selects all elements matched by
    • that are visible.
    • $(“li:hidden”) – Selects all elements matched by
    • that are hidden.
    • $(“:radio”) – Selects all radio buttons in the form.
    • $(“:checked”) – Selects all checked boxex in the form.
    • $(“:input”) – Selects only form elements (input, select, textarea, button).
    • $(“:text”) – Selects only text elements (input[type=text]).
    • $(“li:eq(2)”) – Selects the third
    • element
    • $(“li:eq(4)”) – Selects the fifth
    • element
    • $(“li:lt(2)”) – Selects all elements matched by
    • element before the third one; in other words, the first two
    • elements.
    • $(“p:lt(3)”) – selects all elements matched by

      elements before the fourth one; in other words the first three

      elements.

    • $(“li:gt(1)”) – Selects all elements matched by
    • after the second one.
    • $(“p:gt(2)”) – Selects all elements matched by

      after the third one.

    • $(“div/p”) – Selects all elements matched by

      that are children of an element matched by

      .

    • $(“div//code”) – Selects all elements matched by that are descendants of an element matched by
      .

    • $(“//p//a”) – Selects all elements matched by that are descendants of an element matched by

    • $(“li:first-child”) – Selects all elements matched by
    • that are the first child of their parent.
    • $(“li:last-child”) – Selects all elements matched by
    • that are the last child of their parent.
    • $(“:parent”) – Selects all elements that are the parent of another element, including text.
    • $(“li:contains(second)”) – Selects all elements matched by
    • that contain the text second.
    • $(“td:gt(4)”) – Finds TD #5 and higher
    • $(“input:not(:checked)”) – Finds all inputs that are not checked
    • $(“div,span,p.myClass”) – Finds the elements that match any of these three selectors.
    • $(“input[id][name$=”man”]”) – Finds all inputs that have an id attribute and whose name attribute ends with man