2
var str = "one man"
 $('#select option:contains("+str+")').attr('selected', 'selected');

is not working for my select box:

<select multiple id ="select">
   <option value="3">one man</option>
   <option value="4">second</option>
</select>

any suggestions?

j08691
  • 204,283
  • 31
  • 260
  • 272
monda
  • 3,809
  • 15
  • 60
  • 84

3 Answers3

4

quote mismatch

var str = "one man";

$('#select option:contains('+str+')').prop('selected', true);

or you could just do:

$('#select').val(str);
adeneo
  • 312,895
  • 29
  • 395
  • 388
1

Yes, use the correct quotes (and prop()):

 $('#select option:contains('+str+')').prop('selected', true);

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272
0

Typo quote mismatch

$('#select option:contains("'+str+'")').prop('selected', 'selected');
                string      ^ var ^   string //added single quote


Use .prop()

Read .prop() vs .attr()

Community
  • 1
  • 1
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107