Ori Refael
Ori Refael

Reputation: 3028

jquery finding an element with known class and value

im trying to get an element using jquery who has a certain class and also a specific value which only one of the elements who have that class have. for example

<div class="asd" value="a"></div>

out of :

<div class="asd"></div>
<div class="asd"></div>
<div class="asd"></div>
<div class="asd"></div>

so far i tried something like : $(".mainCss[value=yay]").index() yay is a string object.

Upvotes: 0

Views: 108

Answers (3)

Dave
Dave

Reputation: 46349

Your issue is not what you think it is. You seem to be confused about how to put a string object inside a selector. What you intended is (probably) this:

$(".mainCss[value='"+yay+"']").index()

which will put the value of the variable yay into the selector. If this variable is unconstrained, you should escape it too.

Upvotes: 2

Scott Selby
Scott Selby

Reputation: 9580

$(".mainCss[value='a']").index()

is more spefically what you wanted to select the div in the question you have the code right though

Upvotes: 0

Prog Mania
Prog Mania

Reputation: 624

$(".mainCss[value='yay']").index()

you need to surround the value with single quotes.

Upvotes: 1

Related Questions