Tutorial :jQuery create an array of objects with a specific class



Question:

Is there an array created somewhere of all the objects with a specific class? I want to find the sixth div with class="clCategory". Is there something like:

$('.clCategory:sixth').css(...);  

or

$('.clCategory')[6].css(...);  

Or do I have to create the array at the same time I programmatically create each of the DIVs with class="clCategory"?


Solution:1

You're looking for eq(x)

It's 0-based, so the 6th would actually be #5

E.g.:

$('.clCategory:eq(5)')  

You can also query for $('.clCategory') and access it at [5] or even .get(5). The difference with either is that you would be getting the DOM HTMLElement, not the jQuery wrapped element as in my previous example.


Solution:2

You have to use nth child selector. See this related post.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »