Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery nth item of id/class [closed]

Tags:

jquery

I'd like to use the id selector:

$("#id")

Is there a way to do this to only the nth element with that ID on the page? i.e.

$("#id:n")
like image 439
tkbx Avatar asked Mar 18 '13 16:03

tkbx


People also ask

How to select nth class in jQuery?

jQuery :nth-of-type() SelectorThe :nth-of-type(n) selector selects all elements that are the nth child, of a particular type, of their parent. Tip: Use the :nth-child() selector to select all elements that are the nth child, regardless of type, of their parent.

How to target nth child in jQuery?

jQuery :nth-child() SelectorThe :nth-child(n) selector selects all elements that are the nth child, regardless of type, of their parent. Tip: Use the :nth-of-type() selector to select all elements that are the nth child, of a particular type, of their parent.

How do I get the second last Li in jQuery?

You need to use "nth-last-child(2)" of jquery, this selects the second last element.

What is EQ jQuery?

jQuery eq() Method The eq() method returns an element with a specific index number of the selected elements. The index numbers start at 0, so the first element will have the index number 0 (not 1).


2 Answers

There can be only ONE element with a given id in a page.

From the HTML norm :

There must not be multiple elements in a document that have the same id value.

Now suppose you want to get the nth element with a given class in your page, you may use eq :

$('.myclass').eq(index)
like image 65
Denys Séguret Avatar answered Nov 06 '22 21:11

Denys Séguret


You can do like this:

$("#id:eq(n)")

But like @dystroy answer, it should be only 1 id in a page so you better using class.

like image 22
Eli Avatar answered Nov 06 '22 20:11

Eli