api.jquery.com icon indicating copy to clipboard operation
api.jquery.com copied to clipboard

Documentation: Behavior of :nth-of-type when combined with another selector

Open blelem opened this issue 10 years ago • 2 comments

It seems to be a rather common pitfall to use :nth-of-type and :nth-child together with a class selector.

tr.row:nth-child(odd)

I see it would be useful that the documentation of :nth-of-type and :nth-child would explicitly warn that this construct won't probably do what a developer not that familiar with selectors would expect.

Note! When combining selectors, there is no notion of order among simple selectors, the following two compound selectors are equivalent:

table.myClass tr.row:nth-child(odd)
table.myClass tr:nth-child(odd).row

They both mean select any tr element that matches all of the following independent conditions:

it is an odd-numbered child of its parent;
it has the class "row"; and
it is a descendant of a table element that has the class "myClass".

This is mainly cut&pasted from : http://stackoverflow.com/questions/5545649/can-i-combine-nth-child-or-nth-of-type-with-another-selector

blelem avatar Aug 19 '15 12:08 blelem

It's probably worth adding a note about this. https://developer.mozilla.org/en-US/docs/Web/CSS/%3Anth-child doesn't have a note about it either - might be worth adding one.

Also note that the examples on https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type are worse at describing :nth-of-type than the :nth-child page is :)

gnarf avatar Aug 19 '15 16:08 gnarf

While we're at it, the selectors introduced in jQuery 1.9 nth-last-child() and nth-last-of-type() should also be covered.

blelem avatar Aug 20 '15 05:08 blelem