tags:

views:

72

answers:

3

why does $('tr:even').addClass('alt'); select from the 1st row and $('tr:nth-child(even)').addClass('alt'); selects from the 2nd row ?

+6  A: 

It's because :even gets even elements using 0-based indexing and :nth-child() uses 1-based indexing.

BBonifield
http://api.jquery.com/even-selector/
artlung
then why $('tr:even').addClass('alt') selects index 1, should not it select index 2 (Even)?
amipax
A: 

maybe the selector looks at the index and nth-child(even) looks at the length? Seems like an oversight.

hunter
A: 

got it! :odd and :even selectors use Javascript native zero based numbering. so the first row counts as 0(even)

amipax