user3819410
user3819410

Reputation: 65

How to get listbox all values in jquery?

How to get listbox all values in jquery ?

i have HTML code like this

<div class="right" style="width: 300px">
<fieldset class="field_set">
    <legend>Add Area</legend>
    <select id="addArea-list" multiple="multiple" style="width: 250px"></select>
    <br/> <a href="#" id="addArea">Add Multiple Areas</a>

</fieldset>

i'm adding listbox values in button click in jquery like below

     var value = $("#Area_name").val(); // value added 
     $("#addArea-list").append($('<option></option>').attr('value', value).text(value));

How to get listbox all values in jquery .

i have tried following but i get it all values append returns single value

    var floors = [];
$("#addArea-list").each(function () {
    debugger;
    floors.push({
        FloorName: $(this).text()
    });
});

if my list box have following vales means text1 , text2 , text3

it returns text1text2text3 ... how to get each values ?

Upvotes: 2

Views: 2572

Answers (1)

George
George

Reputation: 36784

Use jQuery's map(), which will iterate through the matches and push to jQuery collection the value returned from the function. Convert the jQuery object returned into a standard Array using .get():

var floors = $("#addArea-list option").map(function () {
    return this.value;
}).get();

JSFiddle

Upvotes: 3

Related Questions