Reputation: 3306
Here is my HTML:
<select name="Pcolor" id="image" style="height:30px;">
<option value="">Избран цвят: Grey M/Navy </option>
<option value="48503341" color-number="0">Black/Charcoal</option>
<option value="48503326" color-number="1">Charcoal M/Blk</option>
<option value="48503325" color-number="2">Grey M/Navy</option>
<option value="48503351" color-number="3">Navy/Grey M</option>
</select>
Here is my Javascript:
var TargetText = "Charcoal M/Blk";
var ColorSelectt = $('#image').find('option:contains(TargetText)').attr("color-number");
alert(ColorSelectt);
With the Javascript code shown i get the attribute from the select menu option, but only when i target Grey M/Navy
i get response undefined
. With all other cases the code works well, why ?
Can you help me out resolve this problem?
Thanks in advance!
Upvotes: 0
Views: 63
Reputation: 6031
use below code. you need to concat TargetText
variable using +
NOTE : as discuss answer is updated to skip first option in search using :gt(0)
check DEMO
var TargetText = "Grey M/Navy";
var ColorSelectt = $('#image').find('option:gt(0):contains("'+TargetText+'")').attr("color-number");
alert(ColorSelectt);
Upvotes: 4