nth-last-of-type selector
Description: Selects all the elements that are the nth-child of their parent in relation to siblings with the same element name, counting from the last element to the first.
-
version added: 1.9jQuery( ":nth-last-of-type(index/even/odd/equation)" )
index: The index of each child to match, starting with the last one (
1
), the stringeven
orodd
, or an equation ( eg.:nth-last-of-type(even)
,:nth-last-of-type(4n)
)
Because jQuery's implementation of :nth-
selectors is strictly derived from the CSS specification, the value of n
is "1-indexed", meaning that the counting starts at 1. For other selector expressions such as :eq()
or :even
jQuery follows JavaScript's "0-indexed" counting. Given a single <ul>
containing three <li>
s, $('li:nth-last-of-type(1)')
selects the third, last, <li>
.
Further discussion of this usage can be found in the W3C CSS specification.
Examples:
Find the second to last li in each matched ul and note it.
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
|
|
Demo:
This is a playground to see how the selector works with different strings.
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
70
71
72
73
74
75
76
77
78
79
80
81
|
|