jQuery eq() function

a not well known trick on jquery eq() function

Andrea Usseglio
Frontend and beyond
1 min readJul 16, 2013

--

As the jQuery documentation says the eq(index) function

Reduce the set of matched elements to the one at the specified index

Using this function you can easily select an object from a set of them.

for example:

$(‘li’).eq(2).hide();

simply select the second li and set it to hide.

But the eq() function has another powerful usage.

Let’s say that you have a collection of some elements and you need to select the penultimate of them. With eq() you can simply write:

$(‘li’).eq(-2)

to select the right element.

Providing a negative index number indicates a position starting from the end of the set.

for more detailed information check the official documentation.

--

--

Andrea Usseglio
Frontend and beyond

Web and mobile application developer. Javascript ninja, Arduino hacker wannabe and Nodejs entusiast..