ani0904071
ani0904071

Reputation: 368

how to dynamically pass a variable in 'options:contains' in jQuery to get the value

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

Answers (1)

ducdhm
ducdhm

Reputation: 1952

You should do like this:

var textVar = 'sometext';

var value = $("#someId").find("option:contains(" + textVar  + ")").val();

alert("value: " + value);

Upvotes: 1

Related Questions