0

I've got a combo box with options, what I want is a function where I send one of the options, and the combo box set that option to the selected one, showing it.

I try

$("#cmbStrategies option:selected").html('Condor');

It works set Condor as the selected option, but adding it has a new option, resulting in two option Condor.

I want just select the option but not adding a new one.

Ram
  • 143,282
  • 16
  • 168
  • 197
Diego Unanue
  • 6,576
  • 8
  • 47
  • 67
  • 1
    Have a look at this : http://stackoverflow.com/questions/496052/jquery-setting-the-selected-value-of-a-select-control-via-its-text-description – Phil-R Dec 12 '13 at 18:53
  • I found the answer in another post: http://stackoverflow.com/questions/4757198/jquery-how-to-select-dropdown-list-item-by-text – Diego Unanue Dec 12 '13 at 19:40

3 Answers3

0

I think you may want to use .val('Condor') not .html().

dball
  • 374
  • 2
  • 10
0
$("#cmbStrategies").val('Condor'); should do the trick.

$("#cmbStrategies option:selected").html('Condor');

This jQuery selector is um "wonky".

Mark
  • 3,123
  • 4
  • 20
  • 31
  • $("#cmbStrategies").val('Condor'); Dosen't set the selected text of the combo box to 'Condor'. $("#cmbStrategies option:selected").html('Condor'); – Diego Unanue Dec 12 '13 at 19:28
  • @DiegoUnanue It will if you have an ``, if you have a different value for `Condor', then use it's value – Mark Dec 12 '13 at 19:31
  • $("#cmbStrategies option:selected").html('Condor'); works I can see the selected option as 'condor' but duplicates the option. Resulting in two options 'Condor' – Diego Unanue Dec 12 '13 at 19:35
  • @DiegoUnanue Why are you adding in the `option:selected` use jQuery sector of the select box, and set it's value. With the selector you used above, you are pointing in another area, plus you have gone back to html. – Mark Dec 12 '13 at 19:43
0

This worked for me:

$("#myDropDown option:contains(myText)").attr('selected', true);

Diego Unanue
  • 6,576
  • 8
  • 47
  • 67