Choose jquery class

I have a class called formStyle that happens several times on my web page. Is it possible to choose let say the first and third (with jquery), if they are five

+4
source share
4 answers

yep like this

$('.formStyle:eq(0), .formStyle:eq(2)') 
+5
source

You can use jQuery :eq() -selector , but it’s easier (and probably faster because of your own selectors) to select all of them and you need to select them later:

 var elements = $('.formStyle'); elements.eq(0) // first elements.eq(2) // third 
+6
source

You can use the eq selector to get a specific index

 $('.formStyle:eq(0), .formStyle:eq(2)') // Will return the first and 3rd elements with the class formStyle 
+5
source

It would be much better for you to provide these elements with unique identifiers and directly refer to them by identifier. They can still use the same class.

+2
source

All Articles