Selecting multiple <parameters> with jQuery

This question asks how to get all the selected <options> of the <select> element and return their values ​​to a comma-separated list. JQuery - multiple choice options

I would like to do the opposite of this question; I have a list of values ​​for a <select> , separated by commas, and I would like to select each <option> whose value is in a comma-separated list using jQuery.

Sample data:

 <script type="text/javascript"> var data = "0a,0d,0f,0g"; </script> <select id="ps-type" name="ps-type" multiple="multiple" size="5"> <option value="0a">Residential - Wall Insulation</option> <option value="0b">Residential - Attic /Crawl Space Insulation</option> <option value="0c">Residential - Foundation Insulation</option> <option value="0d">Residential - Exterior Roof System</option> <option value="0e">Commercial - Wall Insulation</option> <option value="0f">Commercial - Air Barrier System (Walltite)</option> <option value="0g">Commercial - Roof System</option> </select> 
+4
source share
1 answer

.val() also takes an array of values ​​as an argument. Thus, to select multiple values, simply convert the list of values ​​separated by commas to an array using split(",") and pass it to .val() :

 var data = "0a,0d,0f,0g"; $("#ps-type").val(data.split(",")); 

Demo .

+8
source

Source: https://habr.com/ru/post/1434321/


All Articles