Giuseppe Giuseppe - 11 months ago 60
jQuery Question

JQM Custom Select not select dynamically items

I'm using the following code in JQuery Mobile 1.4.5 Page, it show a jqm multiple custom select menu.

<select id="myselect" multiple="multiple" data-native-menu="false">
<option value="empty">options title</option>
<option value="a1">option 1</option>
<option value="a2">option 2</option>
<option value="a3">option 3</option>
</select>


When pagecontainershow occurs I'm searching code snippet to select/check dinamically the multiple options.
It seems in "Selectmenu Widget Api Reference" there aren't any methods.

The component is very good for my purpose, but I need also to control it by javascript (because I have to save to localstorage the items selected and re-select it again on page loading)

https://jsfiddle.net/2s77oc88/3/

Any suggestion?

Answer Source

There are many ways to do it here is one EXAMPLE
You can select different options by their value using the .val() method.

Like so for example: $("#myselect").val("a2").change();

You have to add the .change() method tho to update the Select Menu.