monda monda - 2 months ago 20
Javascript Question

jQuery: get the selected text as comma separated from select box

<select id="testID" multiple="multiple">
<option value="1">test Value1</option>
<option value="2">test Value2</option>
<option value="3">test Value3</option>
<option value="4">test Value4</option>
<option value="5">test Value5</option>
<option value="6">test Value6</option>
</select>

<input type="button" value="Get dropdown selected Value" id ="select-values">


i want to get the comma separated selected text.

I tried ,

$("#select-values").click(function () {
$("select option:selected").text();
});


lets say i have selected first two select options
but i am getting the value
test Value1test Value2


i want it to be
test Value1,test Value2

Answer

Try

fiddle demo

$("#select-values").click(function () {
    var option_all = $("select option:selected").map(function () {
        return $(this).text();
    }).get().join(',');
    console.log(option_all);
});
Comments