Search code examples
javascriptjquerycomboboxjquery-select2jquery-select2-4

How do I show the selected value of select2?


I am trying to set the value dynamically to my combobox on which I have applied select2 jQuery class. After applying code it sets the value to that combobox; but it still says "Select one". Can anyone help me to show the selected value? Please.

HTML:

<select id="cmboDemo">
 <option value="">Select one</option>
 <option value="Bob">Bob</option>
 <option value="Scott">Scott</option>
</select>

Javascript:

$("#cmboDemo").select2();
//....
$("#cmboDemo").val("Scott");
...//    

In result scott is selected but combobox shows "Select one".


Solution

  • Remove $("#cmboDemo").select2();

    and use the second one only:

    $("#cmboDemo").val("Scott");