I am trying to grab all selected items in the following select multiple and separate them by a comma. The code is below:
<select id="ps-type" name="ps-type" multiple="multiple" size="5"> <option>Residential - Wall Insulation</option> <option>Residential - Attic /Crawl Space Insulation</option> <option>Residential - Foundation Insulation</option> <option>Residential - Exterior Roof System</option> <option>Commercial - Wall Insulation</option> <option>Commercial - Air Barrier System (Walltite)</option> <option>Commercial - Roof System</option> </select>
The result I am looking for is the following:
Residential - Wall Insulation, Commercial - Wall Insulation, ...
With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.
Selecting multiple options vary in different operating systems and browsers: For windows: Hold down the control (ctrl) button to select multiple options. For Mac: Hold down the command button to select multiple options.
multiSelect is a jQuery plugin for converting an DIV element into a select list which allows you to select multiple items from a dropdown list as like a tag/token input.
You can use the :selected
selector, and the inline $.map()
function as part of your chain.
$("option:selected").map(function(){ return this.value }).get().join(", ");
Add the values to an array and use join
to create the string:
var items = []; $('#ps-type option:selected').each(function(){ items.push($(this).val()); }); var result = items.join(', ');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With