Getting all options in a select element

html, html-select, jquery

Solution

That code does exactly what it says on the box. Normally I would expect you would have used `$("#wordList option")` to get the options. I am guessing that if the options are "This", "That" and "The other" then you got `ThisThatThe Other,`, which is what that code will do (that is all the text inside the #wordList element, which essentially includes all the options inside that element. The array you are performing `.each()` on has a single element: "ThisThatThe Other", you iterate over it once and add a comma).

You want to concatenate the text of each of the OPTIONS in #wordList (I think), so try

var str = "";    
$("#wordList option").each(function () {    
    str += $(this).text() + ",";    
});    
alert(str);

to give you a string of all the words (like `This,That,The Other,`) If you want an array, instead do this:

var arr = [];    
$("#wordList option").each(function () {    
    arr.push($(this).text());    
});    
alert(arr.join(", "));

Problem

I have a select element where a user can add and remove options. When the user is done they will then click save but I cannot figure out how to then get all the options from the select. I have found JQuery to get all the selected but I just need all of them. I tried this: ``` var str = ""; $("#wordList").each(function () { str += $(this).text() + ","; }); alert(str); ``` But it just concatenates all the option to one long string that ends in a comma.

Original source