pencilCake
pencilCake

Reputation: 53243

first-child selector means what? Select the first-child of the element? Or select it if it is the first-child?

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,

Upvotes: 1

Views: 301

Answers (2)

Reigel Gallarde
Reigel Gallarde

Reputation: 65264

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

source: here.

Upvotes: 0

Parrots
Parrots

Reputation: 26882

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.

Upvotes: 2

Related Questions