Reputation: 16095
I have a class called formStyle which occurs several times in my webpage. Is it possible to select let's say first and third (with jquery) if they are five
Upvotes: 4
Views: 164
Reputation: 35793
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
Upvotes: 5
Reputation: 5259
You would be much better off giving those elements unique IDs and referencing them by the ID directly. They can still share the same class.
Upvotes: 2
Reputation: 30986
You can use jQuery's :eq()
-selector, but it is easier (and probably faster, due to native selectors) to select all of them and pick the ones you need afterwards:
var elements = $('.formStyle');
elements.eq(0) // first
elements.eq(2) // third
Upvotes: 6