Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pre-select ALL options in a jquery mobile dropdown data-bound by knockout?
    primarykey
    data
    text
    <p>Could someone please show me how to preselect all items in a knockout data-bound select with multiple selection tag set?</p> <p>I've tried using this code:</p> <pre class="lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" /&gt; &lt;script src="http://code.jquery.com/jquery-1.6.4.min.js"&gt;&lt;/script&gt; &lt;script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="js/knockout-2.0.0.debug.js"&gt;&lt;/script&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1" /&gt; &lt;meta name="apple-mobile-web-app-capable" content="yes"&gt; &lt;meta charset="utf-8" http-equiv="X-UA-Compatible" content="IE=9" /&gt; &lt;meta http-equiv="CACHE-CONTROL" content="NO-CACHE" /&gt; &lt;/head&gt; &lt;body&gt; Choose a company: &lt;select id="companies" data-role="select" data-bind="options: companies, optionsCaption: 'Choose...', optionsText: 'companyName', optionsValue: 'companyValue', selectedOptions: chosencompanies" data-native-menu="false" multiple&gt;&lt;/select&gt; &lt;input data-bind="value: chosencompanies"&gt;&lt;/input&gt; &lt;script type="text/javascript"&gt; $(document).ready(function () { function company(name, value) { this.name = ko.observable(name); this.value = ko.observable(value); } var viewModel = { companies: ko.observableArray([ { companyName: "test", companyValue: 0 }, { companyName: "test1", companyValue: 1 }, { companyName: "test2", companyValue: 2 }, { companyName: "test5", companyValue: 5 }, { companyName: "test7", companyValue: 7 }, ]), chosencompanies: ko.observableArray(), resetcompanies: function () { this.chosencompanies(null) } }; ko.applyBindings(viewModel); $("#companies option").prop('selected', true); $("#companies").selectmenu(); $("#companies").selectmenu('refresh', 'true'); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Even though I'm using <em>$("#companies option").prop('selected', true)</em> it seems to <strong>only</strong> affect the visual appearance of the dropdown control and not the data-bound chosenCompanies varialbe which is what I need set? :(whose value you can see also since it is data-bound to the input field beneath the dropdown) And before someone proposes please, I realize that I could initialize the observableArray with a comma separated value list, but what when my selection options are dynamic and I don't know with what values I should preinit the observableArray?</p> <p>TIA</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload