Qiao
Qiao

Reputation: 17049

jQuery select n-first elements

I have a tr element

$('tr')

I need to select several first children (td in this case).

How can I do this?
:nth-child() can select even or odd elements. :eq() can select only one.

So the only way is

$('tr td:eq(0), tr td:eq(1), ... tr td:eq(n-1)')

Is there any shortcut?

Upvotes: 1

Views: 752

Answers (1)

JohnP
JohnP

Reputation: 50019

$('tr td:lt(8)')

Use the less than selector

Also, note the comment in the docs

Because :lt() is a jQuery extension and not part of the CSS specification, queries using :lt() cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use $("your-pure-css-selector").slice(0, index) instead.

Upvotes: 4

Related Questions