Reputation: 1687
This may seem easy to the experts out there,
But, how can I go about converting the contents of a drop down (select box) and store them into a comma separated var?
[DROP DOWN]
abdcef1
ghijklm2
nopqrs3
tuvwx4
yz0007
expected result:
var x = abdcef1,ghijklm2,nopqrs3,tuvwx4,yz0007
Much thanks and appreciation for all your help and support.
Cheers,
Jay
Upvotes: 1
Views: 825
Reputation: 1253
try:
<script>
function getSelectedValues() {
var result = "";
var mysel = document.getElementById("myselect");
for (var i = 0; i < mysel.options.length ; i++) {
result = result + mysel.options[i].text + ",";
}
result = result.substring(0,result.length-1);
return result;
}</script>
where
<select id="myselect">
<option value="abdcef1">abdcef1</option>
<option value="ghijklm2">ghijklm2</option>
<option value="nopqrs3">nopqrs3</option>
<option value="tuvwx4">tuvwx4</option>
<option value="yz0007">yz0007</option>
</select>
Upvotes: 1
Reputation: 4133
using jQuery:
var options = [];
$('select#dropdown option').each(function(){
options.push($(this).val());
});
options.join(", ");
Upvotes: 0