Reputation: 368
I want to get value of a select tag's option by text. when I am putting the variable, it is not getting any value. But when I am giving the text manually in single quote it is working. But I want this to be dynamic.
This works fine:
var value = $("#someId").find("option:contains('sometext')").val()
alert("value: "+value) // this works fine
but this does not:
var textVar = 'sometext'
var value = $("#someId").find("option:contains(textVar)").val()
alert("value: "+value) // this doesn't work fine
Upvotes: 1
Views: 682
Reputation: 1952
You should do like this:
var textVar = 'sometext';
var value = $("#someId").find("option:contains(" + textVar + ")").val();
alert("value: " + value);
Upvotes: 1