Here we are going to select nth child of table row as shown below.
We can select the element using “:nth “ selector.
“:nth” Selector:
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
containing six
s, $(
"td:nth-child(3)" )
selects
the first
while $("td:eq(3)")
selects the second.However the code will look like :
$("tr td:nth-child(2)") or
$("table td").eq(n)
The
:nth-child(n)
pseudo-class is easily confused with :eq(n)
,
even though the two can result in dramatically different matched elements. With :nth-child(n)
, all
children are counted, regardless of what they are, and the specified element is
selected only if it matches the selector attached to the pseudo-class. With :eq(n)
only the selector attached to the
pseudo-class is counted, not limited to children of any other element, and the (n+1) th one (n is 0-based) is selected.