views:

428

answers:

6

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, ...

Thanks for the help. -B

+1  A: 

Something like this should do the trick:

var result = "";
$('#ps-type option:selected').each(function(i, item){ 
   result += $(this).val() + ", ";
});
Raul Agrait
+2  A: 
var list = "";
$('#ps-type option:selected').each(function(){
  list += this.value + ", ";
});
return list.substr(0, list.length - 2);
Russell Giddings
you're missing <code>:selected</code> after "option" else you would select every option, and not just the ones selected
Luke Duddridge
Good spot, added it now :-)
Russell Giddings
+4  A: 

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(', ');
Guffa
You need the text() and not the val()
kgiannakakis
`val()` will actually still get the text from an option if there is no value attribute.
Mark B
I just tried it out, the val() works just fine.
Raul Agrait
@kgiannakakis: Unless there is a value specified, the value is the text. If there is a value specified, you would most likely want the value instead.
Guffa
A: 

Here you go:

var result = new Array();

$("#ps-type option:selected").each(function() {
    result.push($(this).val());
});

var output = result.join(", ");
Mark B
+4  A: 

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(", ");
Jonathan Sampson
Agreed. For full jQuery sexiness, map() is definitely the way to go.
patrick dw
Full jQuery Sexiness is mandatory ;)
Jonathan Sampson
beat me to it, .map() way more elegant!
F.Aquino
+2  A: 

On a multiple select element, the val command of the select element will return an array of the selected options values. If no values are present, the text of the element is used:

var output = $("#ps-type").val().join(', ');

update: However, when there are no selected options val() returns null not an empty array. One way to get around this:

var output = ($("#ps-type").val() || []).join(', '); 

You can play around with it in this demo I put together.

From the docs:

In the case of <select multiple="multiple"> elements, the .val() method returns an array containing each selected option.

Doug Neiner
Errors when no options are selected though, right?
Russell Giddings
@Russell, good point. I updated my answer to accommodate a non selection. There are a ton of other ways as well.
Doug Neiner