2

It says on here (http://api.jquery.com/first-selector) that " To achieve the best performance when using :first to select elements, first select the elements using a pure CSS selector, then use .filter(":first") "

Can someone pls give an example of this?

1
  • 2
    Not an answer, but if you are worried about performance: drop jQuery for Vanilla JS. Commented Dec 1, 2012 at 22:13

2 Answers 2

2

Example collecting all DIV , then filtering :first

var divFirst=$('div').filter(':first');

Can also use first() method which will also use filter() internally in jQuery

var divFirst=$('div').first();
Sign up to request clarification or add additional context in comments.

Comments

1
$('.elements').filter(':first'); 

or:

$('.elements').first();     

or:

$('.elements').eq(0); 

More efficient than:

$('.elements:first');

This is the case with other jQuery selectors like :has versus has method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.