.is( selector )Returns: Boolean
Description: Check the current matched set of elements against a selector, element, or jQuery object and return true
if at least one of these elements matches the given arguments.
-
version added: 1.0.is( selector )
-
selectorType: SelectorA string containing a selector expression to match elements against.
-
-
version added: 1.6.is( function )
-
functionA function used as a test for every element in the set. It accepts two arguments,
index
, which is the element's index in the jQuery collection, andelement
, which is the DOM element. Within the function,this
refers to the current DOM element.
-
-
version added: 1.6.is( selection )
-
selectionType: jQueryAn existing jQuery object to match the current set of elements against.
-
-
version added: 1.6.is( elements )
-
elementsType: ElementOne or more elements to match the current set of elements against.
-
Unlike other filtering methods, .is()
does not create a new jQuery object. Instead, it allows you to test the contents of a jQuery object without modification. This is often useful inside callbacks, such as event handlers.
Suppose you have a list, with two of its items containing a child element:
1
2
3
4
5
|
|
You can attach a click handler to the <ul> element, and then limit the code to be triggered only when a list item itself, not one of its children, is clicked:
1
2
3
4
5
6
|
|
Now, when the user clicks on the word "list" in the first item or anywhere in the third item, the clicked list item will be given a red background. However, when the user clicks on item 1 in the first item or anywhere in the second item, nothing will occur, because in those cases the target of the event would be <strong>
or <span>
, respectively.
Using a Function
The second form of this method evaluates expressions related to elements based on a function rather than a selector. For each element, if the function returns true
, .is()
returns true
as well. For example, given a somewhat more involved HTML snippet:
1
2
3
4
5
6
7
8
|
|
You can attach a click handler to every <li>
that evaluates the number of <strong>
elements within the clicked <li>
at that time like so:
1
2
3
4
5
6
7
8
9
10
11
|
|
Examples:
Shows a few ways is() can be used inside an event handler.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
|
Demo:
Returns true, because the parent of the input is a form element.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
|
Demo:
Returns false, because the parent of the input is a p element.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
|
Demo:
Checks against an existing collection of alternating list elements. Blue, alternating list elements slide up while others turn red.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
|
Demo:
An alternate way to achieve the above example using an element rather than a jQuery object. Checks against an existing collection of alternating list elements. Blue, alternating list elements slide up while others turn red.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
|