John John - 29 days ago 15
Javascript Question

Question about JQuery - Get values of some selected items by class

Hey Guys,
I have a form with 4 select tags:

<select class="section_1">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
<select class="section_1">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
<select class="section_1">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<select class="section_1">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>


I want to get by jquery all the classes of "section_1" and their selected options value.

I got now: $(".section_1")... by how I continue?

Thanks!

Answer

You can use .each for looping through that

$(".section_1").each(function(){
    alert(this.value);
});

or you can use .map()

var selectedValues = $(".section_1").map(function(){
    return this.value;
}).get().join(',');

You can see a working demo