Jquerycheatsheet.com Logo
    • Selectors Basics
      • *
      • jQuery API

        All Selector (“*”)

        Select all elements inside the document:

        jQquery Example
         
        Select all elements inside the document:
        
        $("*")
        
      • .class
      • jQuery API

        Class Selector (“.class”)

        The .class selector selects all elements with the specific class. The class refers to the class attribute of an HTML element.

        jQquery Example
         
        Select all elements with class "main":
        
        $(".main")
        
      • element
      • jQuery API

        Element Selector (“element”)

        The element selector selects all elements with the specific element name.

        jQquery Example
         
        Select all <h2> elements:
        
        $("h2")
        
      • #id
      • jQuery API

        ID Selector (“#id”)

        The #id selector selects the element with the specific id. The id refers to the id attribute of an HTML element.

        jQquery Example
         
        Select the element with the id "card":
        
        $("#card")
        
      • selector1, selector2, selectorN
      • jQuery API

        Multiple Selector (“selector1, selector2, selectorN”)

        The element selector can also be used to select multiple elements.
        Note: Seperate each element with a comma.

        jQquery Example
         
        Select all <h2>, <div> and <span> elements:
        
        $("h2, div, span")
        
    • Selectors Hierarchy
      • parent > child 2.2
      • jQuery API

        Child Selector (“parent > child”)

        Selects all elements that are a direct child of the specified element

        jQquery Example
         
        Select all <span> elements that are a direct child of a <div> element:
        
        $("div > span")
                    
      • ancestor descendant
      • jQuery API

        Descendant Selector (“ancestor descendant”)

        Selects all elements that are descendants of a given ancestor. A descendant of an element could be a child, grandchild, great-grandchild, etc, of that element.

        jQquery Example
         
        Select all <span> elements that are descendants of a <div> element:
        
        $("div span")
        
      • prev + next
      • jQuery API

        Next Adjacent Selector (“prev + next”)

        Selects the "next" element of the specified "element". The "next" element must be placed right after the specified "element" to be selected.

        jQquery Example
         
        Select the <p> element that are next to each <div> element:
        
        $("div + p")
        
      • prev ~ siblings
      • jQuery API

        Next Siblings Selector (“prev ~ siblings”)

        Selects sibling elements that appear after the specified "element". Note: Both of the specified elements must share the same parent.

        jQquery Example
         
        Select all <p> elements that are siblings and appear after the <div> element:
        
        $("div ~ p")
        
    • Selectors Basic Filters
      • :animated
      • jQuery API

        :animated Selector

        Select all elements that are in the progress of an animation at the time the selector is run.

        jQquery Example
         
        Selects any element that is currently animated:
        
        $(":animated")
        
      • :eq()
      • jQuery API

        :eq() Selector

        Selects an element with a specific index number. The index numbers start at 0, so the first element will have the index number 0 (not 1).

        jQquery Example
         
        Select the second <p> element:
        
        $("p:eq(1)")
        
      • :even
      • jQuery API

        :even Selector

        Selects each element with an even index number (0, 2, 4, etc.). The index numbers start at 0.

        jQquery Example
         
        Select every other (even) <tr> element:
        
        $("tr:even")
        
      • :odd
      • jQuery API

        :odd Selector

        Selects each element with an odd index number (1, 3, 5, etc.). The index numbers start at 0. This is mostly used together with another selector to select every odd indexed element in a group (like in the example below).

        jQquery Example
         
        Select every other (odd) <tr> element:
        
        $("tr:odd")
        
      • :first
      • jQuery API

        :first Selector

        Selects the first element. Note: This selector can only select one single element. Use the :first-child selector to select more than one element (one for each parent).

        jQquery Example
         
        Select the first <p> element:
        
        $("p:first")
        
      • :last
      • jQuery API

        :last Selector

        Selects the last element. Note: This selector can only select one single element. Use the :last-child selector to select more than one element (one for each parent).

        jQquery Example
         
        Select the last <p> element:
        
        $("p:last")
        
      • :header
      • jQuery API

        :header Selector

        Selects all header elements (<h1> to <h6>).

        jQquery Example
         
        Select all header elements (<h1> to <h6>):
        
        $(":header")
        
      • :lang()
      • jQuery API

        :lang() Selector

        Selects all elements with the language attribute starting with a specified value. Note: The value has to be a whole word, either alone, like lang="en", or followed by a hyphen( - ), like lang="en-us".

        jQquery Example
         
        Select all <p> elements with a lang attribute value that starts with "de":
        
        $("p:lang(de)")
        
      • :not()
      • jQuery API

        :not() Selector

        Selects all elements except the specified element. This is mostly used together with another selector to select everything except the specified element in a group (like in the example below).

        jQquery Example
         
        Select all <p> elements except those with class="intro":
        
        $("p:not(.intro)")
        
      • :root
      • jQuery API

        :root Selector

        Selects the document's root element. In HTML, the root element is always the <html> element.

        jQquery Example
         
        Set the background color for the HTML document to blue:
        
        $(":root").css("background-color", "blue");
        
      • :target
      • jQuery API

        :target Selector

        Selects the target element indicated by the fragment identifier of the document's URI. This kind of URI ends with a "number sign" (#) followed by an anchor identifier (called the fragment identifier).

        jQquery Example
         
        Select this URL's URI https://example.com/#foo
        
        $(":target")
        
    • Selectors Content Filters
      • :contains()
      • jQuery API

        :contains() Selector

        Selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example below).

        jQquery Example
         
        Select all <button> elements containing "share":
        
        $("button:contains(share)")
        
      • :empty
      • jQuery API

        :empty Selector

        Selects empty elements. An empty element is an element without child elements or text.

        jQquery Example
         
        Select all empty elements:
        
        $(":empty")
        
      • :has()
      • jQuery API

        :has() Selector

        Selects all elements that have one or more elements inside of them, that matches the specified selector.

        jQquery Example
         
        Select all <p> elements that have a <span> element inside of them:
        
        $("p:has(span)")
        
      • :parent
      • jQuery API

        :parent Selector

        Selects all elements that are the parent of another element, including text nodes.

        jQquery Example
         
        Select all <td> elements with children, including text:
        
        $("td:parent")
        
    • Selectors Visibility
      • :hidden
      • jQuery API

        :hidden Selector

        Selects hidden elements. Hidden elements are elements that are:

        • Set to display:none
        • Form elements with type="hidden"
        • Width and height set to 0
        • A hidden parent element (this also hides child elements)

        jQquery Example
         
        Show hidden <p> elements:
        
        $("p:hidden").show();
        
      • :visible
      • jQuery API

        :visible Selector

        Selects every element that is currently visible. Visible elements are elements that are not:

        • Set to display:none
        • Form elements with type="hidden"
        • Width and height set to 0
        • A hidden parent element (this also hides child elements)

        jQquery Example
         
        Select all visible <p> elements:
        
        $("p:visible")
        
    • Selectors Attributes
      • [name|=”value”]
      • jQuery API

        Attribute Contains Prefix Selector [name|=”value”]

        Selects elements that have the specified attribute with a value either equal to a given string or starting with that string followed by a hyphen (-).

        jQquery Example
         
        Select all <p> elements with a title attribute that starts with the value "Today":
        
        $("p[title|='Today']")
        
      • [name*=”value”]
      • jQuery API

        Attribute Contains Selector [name*=”value”]

        Selects elements that have the specified attribute with a value containing a given substring.

        jQquery Example
         
        Select all <input> elements with a name attribute that contains the word "country":
        
        $("input[name*='country']")
        
      • [name~=”value”]
      • jQuery API

        Attribute Contains Word Selector [name~=”value”]

        Selects elements that have the specified attribute with a value containing a given word, delimited by spaces.

        jQquery Example
         
        Select all <input> elements with a name attribute that contains the specific word "age":
        
        $("input[name~='age']")
        
      • [name$=”value”]
      • jQuery API

        Attribute Ends With Selector [name$=”value”]

        Selects elements that have the specified attribute with a value ending exactly with a given string. The comparison is case sensitive.

        jQquery Example
         
        Select all <a> elements with a href attribute that ends with ".org":
        
        $("a[href$='.org']")
        
      • [name=”value”]
      • jQuery API

        Attribute Equals Selector [name=”value”]

        Selects elements that have the specified attribute with a value exactly equal to a certain value.

        jQquery Example
         
        Select every element containing an input attribute with the value "price":
        
        $("[id='price']")
        
      • [name!=”value”]
      • jQuery API

        Attribute Not Equal Selector [name!=”value”]

        Select elements that either don't have the specified attribute, or do have the specified attribute but not with a certain value.

        jQquery Example
         
        Select all < p > elements NOT containing a class attribute with the value "intro":
        
        $("p[class!='intro']")
        
      • [name^=”value”]
      • jQuery API

        Attribute Starts With Selector [name^=”value”]

        Selects elements that have the specified attribute with a value beginning exactly with a given string.

        jQquery Example
         
        Select all <input> elements with a name attribute that starts with "contact":
        
        $("input[name^='contact']")
        
    • Selectors Child Filters
      • :first-child
      • jQuery API

        :first-child Selector

        Selects all elements that are the first child of their parent.

        jQquery Example
         
        Select every <p> element that is the first child of its parent:
        
        $("p:first-child")
        
      • :first-of-type
      • jQuery API

        :first-of-type Selector

        Selects all elements that are the first child, of a particular type, of their parent.

        jQquery Example
         
        Select every <p> element that is the first <p> element of its parent:
        
        $("p:first-of-type")
        
      • :last-child
      • jQuery API

        :last-child Selector

        Selects all elements that are the last child of their parent.

        jQquery Example
         
        Select every <p> element that is the last child of its parent:
        
        $("p:last-child")
        
      • :last-of-type
      • jQuery API

        :last-of-type Selector

        Selects all elements that are the last child, of a particular type, of their parent.

        jQquery Example
         
        Select every <p> element that is the last <p> element of its parent:
        
        $("p:last-of-type")
        
    • Selectors Forms
    • Attributes Attributes
    • Attributes CSS
    • Attributes Dimensions
    • Attributes Offset
    • Attributes Data
    • Hooks Directives
    • Manipulation Copying
    • Manipulation Insertion
    • Manipulation Removal
    • Manipulation Replacement
    • Traversing Filtering
    • Traversing Miscellaneous
    • Traversing Tree Traversal