Tuesday, November 18, 2014

:eq() selector example-demo in jQuery

jQuery API provides another selector ":eq(n)" which allows to select particular child element from its parent. For example, if you want to select 2nd li element from the ul tag. See below code.
This code will find the 2nd li element and makes it forecolor to "Red".

Note: With eq() selector index starts from 0.

             $('ul li:eq(1)').css('color', 'Red');

The difference between :nth-child() and :eq() is that with :nth-child you can select a series of elements which is not possible with .eq(). .eq(n) will always select the nth element in its parent.





No comments:

Post a Comment