TheBoubou
TheBoubou

Reputation: 19933

Get all dropdown item with separator

I have this:

<select id="CheckList" multiple="multiple">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
</select>

Is it possible with jQuery to get a full list of the options with separator between like this:

1,2,3,4

Upvotes: 0

Views: 1397

Answers (2)

Damian
Damian

Reputation: 1553

var list = ''
$(#CheckList).children.each(function() {
   list += this.value + ','
}

accidentally posted this befoere finishing. meant to say something like this. But the guy above has the solution.

Upvotes: 0

cletus
cletus

Reputation: 625307

Use map() and join() but you have to use get() on the map() result to convert it into a Javascript array (which has the join() method):

var list = $("#CheckList option").map(function(i, n) {
  return n.value;
}).get().join(",");

Upvotes: 1

Related Questions