简体   繁体   中英

How can i by default set all options selected in multi-select dropdown?

I am populating my multi-select dropdown from json object.

Here is my select dropdown

<select size="2" id="inst_all" style="width:200px;" multiple ng-multiple="true" ng-model="selectedCountries"
ng-options="sort.Institute as sort.Institute for sort in products | unique : 'Institute'">
</select>

I have tried with following jquery snippet, but it didnt worked.

<script>$('#inst_all option').prop('selected', true);</script>

How can i select all options be default?

When using dynamic content from a json file/object you should always make sure the page is fully loaded before doing any actions on DOM elements.
Use $( document ).ready(function() {

 $( document ).ready(function() { $('#inst_all option').prop('selected', true); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select size="2" id="inst_all" style="width:200px;" multiple ng-multiple="true" ng-model="selectedCountries" ng-options="sort.Institute as sort.Institute for sort in products | unique: 'Institute'"> <option>Option 1</option> <option>Option 2</option> </select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM