DOM Element Selection

The selector syntax used by jQuery is a combination of CSS1-3 and XPath selectors.

With jQuery you can select elements by attributes, element type, element position, CSS class, or a combination of these.

The syntax for selecting elements is as follows:

$(selector,[context])

or

jQuery(selector, [context])

To select elements by tag name, use the tag name in the selector. For example, $("div") retrieves all of the divs in a document.

 
<!DOCTYPE html> 
<html>
    <head>
        <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script> 
        <script> 
            $(document).ready(function(){ 
                var wrappedElements = $("div"); 
                document.writeln(wrappedElements.length); 
            });
        </script> 
    </head> 
    <body>
        <div id="1"></div> 
        <div id="2"></div> 
        <div id="3"></div>
    </body> 
</html>
  
Click to view the demo

By default, when selecting elements, jQuery searches through the entire DOM tree. To search through a subtree of the DOM, pass an optional second parameter to the jQuery function to give the selection a context.

Retrieve from a series of links for a single div:

$("a","#div");
 
<html>
  <head>
    <style>
      .test{ border: 1px solid red; }
    </style>
    <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script> 
    <script type="text/javascript">
    $(document).ready(function(){
           alert($(":hidden", document.body).length);
    });
    </script>
  </head>
  <body>
      <span></span>
        <div></div>
        <div style="display:none;">Hider!</div>
        <div></div>
        <div></div>
        <form>
            <input type="hidden" />
            <input type="hidden" />
            <input type="hidden" />
        </form>
        <span></span>
  </body>
</html>
  
Click to view the demo

The basic CSS Selectors supported by jQuery

SelectorDescription
*Matches any element.
EMatches element with tag name E.
E FMatches elements with tag name F that are descendents of E.
E>FMatches elements with tag name F that are direct children of E.
E+FMatches elements F immediately preceded by sibling E.
E~FMatches elements F preceded by any sibling E.
E:has(F)Matches elements with tag name E that have at least one descendent with tag name F.
E.CMatches elements E with class name C. Omitting E is the same as *.C.
E#IMatches element E with id of I. Omitting E is the same as *#I.
E[A]Matches elements E with attribute A of any value.
E[A=V]Matches elements E with attribute A whose value is V.
E[A^=V]Matches elements E with attribute A whose value begins with V.
E[A$=V]Matches elements E with attribute A whose value ends with V.
E[A*=V]Matches elements E with attribute A whose value contains V.
Home 
  JavaScript Book 
    jQuery  

Selector:
  1. jQuery Selector
  2. DOM Element Selection
  3. Filter Selectors
  4. Selecting by Position
  5. Filtering by Content
  6. Filtering by Relationships
  7. Escape characters
  8. ID attribute
  9. Tag Name
  10. Class Name
  11. Descendant
  12. Child (E > F)
  13. General sibling (E ~ F)
  14. Multiple expressions (E, F, G)
  15. Universal (*)
  16. Numbered child (:nth-child(n/even/odd/expr))
  17. First child (:first-child)
  18. Last child (:last-child)
  19. Only child (:only-child)
  20. Not (:not(E))
  21. Empty (:empty)
  22. Attribute selectors
  23. Attribute ([attr])
  24. Attribute equals ([foo=bar])
  25. Attribute does not equal ([foo!=bar])
  26. Attribute begins with ([foo^=bar])
  27. Attribute ends with ([foo$=bar])
  28. Attribute contains ([foo*=bar])
  29. Attribute contains word ([foo~=bar])
  30. Attribute contains prefix ([foo|=bar])
  31. Attribute exists $("[attributeName*='value']");
  32. Form selectors (:input)
  33. Form text fields (input:text)
  34. Form Password field (input:password)
  35. Form Radio button (input:radio)
  36. Form Checkbox (input:checkbox)
  37. Form Submit button (input:submit)
  38. Form Image button (input:image)
  39. Form Reset button (input:reset)
  40. Form button (input:button)
  41. Form File upload (input:file)
  42. Form Enabled form element (input:enabled)
  43. Form Disabled form element (input:disabled)
  44. Form Checked box (input:checked)
  45. Form Selected option (input:selected)
  46. Element at index (:eq(n))
  47. Greater than (:gt(n))
  48. Less than (:lt(n))
  49. First (:first)
  50. Last (:last)
  51. Even element (:even)
  52. Odd element (:odd)
  53. Is parent (:parent)
  54. Contains text (:contains(text))
  55. Contains element (:has(E))
  56. Visible (:visible)
  57. Hidden (:hidden)
  58. Header element (:header)
  59. Currently animating (:animated)
  60. $(this)
  61. Custom User Selectors