I'm using Oleg's select2 demo, but I am wondering whether it would be possible to change the currently selected value in the dropdown menu.
For example, if the four values loaded were: "Any", "Fruit", "Vegetable", "Meat"
and the dropdown list defaulted to "Any"
, how would I be able to change that to "Fruit"
in the JqGrid event loadComplete
?
Is this possible?
if you have ajax data source please refer this for bugs free
https://select2.org/programmatic-control/add-select-clear-items
// Set up the Select2 control
// Fetch the preselected item, and add to the control
If you want to add new
value='newValue'
andtext='newLabel'
, you should add this code:Add new Option to
<select>
:Trigger
<select>
change to selected value:Just wanted to add a second answer. If you have already rendered the select as a select2, you will need to have that reflected in your selector as follows:
if you want to select a single value then use
$('#select').val(1).change()
if you want to select multiple values then set value in array so you can use this code
$('#select').val([1,2,3]).change()
My Expected code :
working perfectly thank to @PanPipes for the usefull one.
Having some troubles with setting a String option selected in a select2:
With the option: "option string1 /option" used:
BUT with an option with a value: option value "E" string1 /option :
Hopes this help someone struggling with the same issue.