Nick
Nick

Reputation: 16095

Select class jquery

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

Answers (4)

Richard Dalton
Richard Dalton

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

Teneff
Teneff

Reputation: 32158

yep, like this

$('.formStyle:eq(0), .formStyle:eq(2)')

Upvotes: 5

cusimar9
cusimar9

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

jwueller
jwueller

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

Related Questions