Wojciech Szabowicz
Wojciech Szabowicz

Reputation: 4198

How to get value from many select boxes inside div using jquery

I've created quite interesting html contraption like this

<div class="alert alert-info" id="1">
    <select name="first" id="sel-1-1">
         <option value="1">One</option>
         <option value="2">Two</option>
    </select>
    <select name="second" id="sel-2-1">
         <option value="1">One</option>
         <option value="2">Two</option>
    </select>
</div>

Now my question is, how to push values of all selected options in div, inside array using jquery? Have no idea where to start.

Upvotes: 1

Views: 1328

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337560

You can use map() to create an array of the selected values:

var selectedValueArray = $('.alert select').map(function() {
    return $(this).val();
}).get();

Example fiddle

Upvotes: 3

Alaa Badran
Alaa Badran

Reputation: 1858

Use this:

var new_item = '<option value="3">Three</option>';
$('.alert select').append(new_item);

Upvotes: 0

Related Questions