views:

68

answers:

2

Look at this:

var selection= $('table td:first-child');

In practice it selects the first row's <td> elements in the table.

When I saw it for the first time, I took it as: Select all first-child elements within all <td> in the <table> </table>.

But what it does is: Select all <td> within the table if it is the first-child of its parent. Means first <td> between each tags.

So, the question is, does first-child selector works like a flag? Or it works like a kind of method to get the first-child of the element in question in the jQuery wrapper-set?

thanks,

+2  A: 

Firstchild will get the TD within the table that are the first child within their parent. If you wanted what you intially though it'd be something like table td > *:first-child. Think of it like the rest of the : filters jQuery provides: hidden, disabled, checked, etc. It applies to the element in the selector it is attached to.

Parrots
A: 

the :first-child selector can match more than one: one for each parent. This is equivalent to :nth-child(1).

source: here.

Reigel