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
  • 197,815
  • 30
  • 248
  • 265
monda
  • 3,667
  • 14
  • 56
  • 82

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
  • 303,455
  • 27
  • 380
  • 377
1

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

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

jsFiddle example

j08691
  • 197,815
  • 30
  • 248
  • 265
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
  • 56,454
  • 22
  • 99
  • 107