<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
Try
fiddle demo
$("#select-values").click(function () {
var option_all = $("select option:selected").map(function () {
return $(this).text();
}).get().join(',');
console.log(option_all);
});
This feels like the quickest way:
$('#testID').val().toString()
I'm too lazy to write a for loop for this
Try:
var selectedItems = ($.map($("select[id*=select-values] option:selected"), function(item, i) { return $(item).text() })).join(", ");
Try something like this
$(document).ready(function () {
$("#select-values").click(function () {
var val = [];
$("select option:selected").each(function () {
val.push(this.text);
});
alert(val.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