Contents
  1. 1. Basic Selectors
  2. 2. Hierarchical Selectors
    1. 2.1. Select Children and Descendant
    2. 2.2. Select After
    3. 2.3. Select all Siblings
  3. 3. Selector Filters
    1. 3.1. Filter by Position
    2. 3.2. Filter by Content
    3. 3.3. Filter by Attribute
    4. 3.4. Filter by Visibility
    5. 3.5. Select Child
    6. 3.6. Select Form
    7. 3.7. :Not
    8. 3.8. Others
  4. 4. Filter Methods
    1. 4.1. Filter Methods
    2. 4.2. Position Method
    3. 4.3. Hierarchical Methods
  5. 5. References

Basic Selectors

Select All

1
$("*")

Select by element id

1
$("#id")  

Select by element class name

1
$(".className")  

Select by element tag name

1
$("div")  

Select by multiple condition union

1
$("selector1,selector2,...,selectorN ")

Select by tag name and class name

1
$('div.my-class')

Hierarchical Selectors

Select Children and Descendant

1
2
$("form > input") // all direct children
$("table td") // select all descendant

Select After

1
$("label + input") // select all input after label

Select all Siblings

1
$("#prev ~ div") // select all siblings

Selector Filters

Filter by Position

1
2
3
4
5
6
7
$("tr:first")
$("tr:last")
$("tr:even")
$("tr:odd")
$("tr:eq(1)")
$("tr:gt(1)") // greater than
$("tr:lt(3)") // less than

Filter by Content

1
2
3
4
5
6
7
8
9
10
11
// :contains
$("div:contains(hello)") // select all div contains text 'hello'

// :empty
$("td:empty") // select all td text is empty

// :has()
$("div:has(p)") // select all div has <p> element

// :parent()
$("div :parent(p)") // select all div contained by <p> element

Filter by Attribute

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// [attr]
$("div[id]") // all <div> have id attr

// [attr="value"]
$("div[id=div1]")

// [attr!="value"] not equal
$("div[id!=div1]")

// [attr^="value"] attr value start with
$("div[id^=test]")] // all div, 'id' attr value start with 'test'

// [att|="value"] attr value equals or start with
$("div[id|=test]")]

// [attr$="value"] attr value end with
$("div[id$=test]") // all div, 'id' attr value end with 'test'

// [attr*="value"] attr value contains
$("div[id*=test]") // all div, 'id' attr value contains 'test'

// [name~="value"] attr value split with whitespace. splited values contains
$( "input[name~='man']" )

Filter by attribute and class name

1
2
$('.myclass[reference="12345"]')
$("input[reference=12345].myclass")

Filter by Visibility

1
2
3
4
5
// :hidden
$("input:hidden")

// :visible
$("input:visible")

Select Child

1
2
3
4
5
6
7
8
9
10
11
// :nth-child
$("ul li:nth-child(2)") // match ul child li second

// :first-child
$("ul li:first-child")

// :last-child
$("ul li:last-child")

// :only-child
$("ul li:only-child") // match ul only contain one li child

Select Form

Filter by element name

1
2
:button	//Selects all button elements and elements of type button.
:input //Selects all input, textarea, select and button elements.

Filter by input element type

1
2
3
4
5
6
7
8
:checkbox
:file
:image
:text
:password
:radio
:reset
:submit

Filter by status of form element

1
2
3
4
5
6
:checked 	//Matches all elements that are checked or selected.
:selected
$("option:selected[value=1]")
:focus //Selects element if it is currently focused.
:disabled
:enabled //Selects all elements that are enabled.

:Not

1
2
3
// :not()
$("input:not(:checked)")
$("#form1 input:not('#id')")

Others

1
2
:header // select all header elements, such as <H1>,<H2>
:animated

Filter Methods

Filter Methods

1
2
3
filter("p") // get all <p> elements in jquery dom set
$("li").has("span") //get all li has child element 'span'
not("p, .name") // get all not <p> and class=name elements in jquery dom set

Position Method

1
2
3
first()
last()
eq("") // index begin with 0

Hierarchical Methods

Select parent or ancestors

1
2
3
4
parent() // one parent
parents() // all ancestors
$("span").parents("ul"); // all ancestors
$("span").parentsUntil("div") // returns all ancestor elements between two given arguments

Select children or descendants

1
2
3
4
children("") 
children()
find(""); // find descendants
// the find() is similar to the children() method,but childern() only find children not descendants.

Select siblings

1
2
3
4
5
6
7
8
siblings() 
siblings("") // get other dom of same level, don't contain self
next()
nextAll()
nextUntil()
prev()
prevAll()
prevUntil()

References

[1] Selectors - jQuery Documentation

Contents
  1. 1. Basic Selectors
  2. 2. Hierarchical Selectors
    1. 2.1. Select Children and Descendant
    2. 2.2. Select After
    3. 2.3. Select all Siblings
  3. 3. Selector Filters
    1. 3.1. Filter by Position
    2. 3.2. Filter by Content
    3. 3.3. Filter by Attribute
    4. 3.4. Filter by Visibility
    5. 3.5. Select Child
    6. 3.6. Select Form
    7. 3.7. :Not
    8. 3.8. Others
  4. 4. Filter Methods
    1. 4.1. Filter Methods
    2. 4.2. Position Method
    3. 4.3. Hierarchical Methods
  5. 5. References