Carlitos Overflow
Carlitos Overflow

Reputation: 683

loop through each select field using jQuery

how can get the value selected by the user on each select field using $(.promote).each(function(){}); the html code is:

<div class="class1">
<select class=" promote">
  <option value="Promote">Promote</option>
  <option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>
<div class="class2">
<select class=" promote">
  <option value="Promote">Promote</option>
  <option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>;
<div class="class3">
<select class=" promote">
  <option value="Promote">Promote</option>
  <option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>;
<div class="class4">
<select class=" promote">
  <option value="Promote">Promote</option>
  <option value="DoNotPromote" selected="selected">Do not Promote</option>
</select>
</div>;

Upvotes: 1

Views: 3360

Answers (5)

Manivannan
Manivannan

Reputation: 3254

instead of each method, you can directly get the values as an array

var selValues = $('.promote').map(function(){return $(this).val()}).get()

Fiddle http://jsfiddle.net/xcsdLapc/1/

Upvotes: 0

Xogle
Xogle

Reputation: 363

var count = 0;
var array = [];

$('.promote').each(function(){
    array[count] = $(this).val();
    count++;
});

Upvotes: 0

Alex Char
Alex Char

Reputation: 33238

You can use a button and on click you get the values of each select using .map()

$("#getValues").on("click", function() {
  var values = $(".promote").map(function() {
    return $(this).val();
  }).get();
  alert(values);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="class1">
  <select class=" promote">
    <option value="Promote">Promote</option>
    <option value="DoNotPromote" selected="selected">Do not Promote</option>
  </select>
</div>
<div class="class2">
  <select class=" promote">
    <option value="Promote">Promote</option>
    <option value="DoNotPromote" selected="selected">Do not Promote</option>
  </select>
</div>
<div class="class3">
  <select class=" promote">
    <option value="Promote">Promote</option>
    <option value="DoNotPromote" selected="selected">Do not Promote</option>
  </select>
</div>
<div class="class4">
  <select class=" promote">
    <option value="Promote">Promote</option>
    <option value="DoNotPromote" selected="selected">Do not Promote</option>
  </select>
</div>
<input type="button" value="Get All Values" id="getValues" />

You can use .each() too:

$("#getValues").on("click", function () {
    var values = [];
    $(".promote").each(function (i) {
       values[i] = $(this).val();
    });
    alert(values);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="class1">
    <select class=" promote">
        <option value="Promote">Promote</option>
        <option value="DoNotPromote" selected="selected">Do not Promote</option>
    </select>
</div>
<div class="class2">
    <select class=" promote">
        <option value="Promote">Promote</option>
        <option value="DoNotPromote" selected="selected">Do not Promote</option>
    </select>
</div>
<div class="class3">
    <select class=" promote">
        <option value="Promote">Promote</option>
        <option value="DoNotPromote" selected="selected">Do not Promote</option>
    </select>
</div>
<div class="class4">
    <select class=" promote">
        <option value="Promote">Promote</option>
        <option value="DoNotPromote" selected="selected">Do not Promote</option>
    </select>
</div>
<input type="button" value="Get All Values" id="getValues" />

The each method is meant to be an immutable iterator, where as the map method can be used as an iterator, but is really meant to manipulate the supplied array and return a new array.

Another important thing to note is that the each function returns the original array while the map function returns a new array. If you overuse the return value of the map function you can potentially waste a lot of memory.

source

Upvotes: 4

thiag0
thiag0

Reputation: 2229

$(".promote").each(function() {
    var $selectBox = $(this);
    var selectedOption = $selectBox.find(":selected").val();
});

JSFiddle: http://jsfiddle.net/acuocgt5/

Upvotes: 0

Lee Wise
Lee Wise

Reputation: 900

$('.promte').each(function(){
  // this.value;
  // OR
  // $(this).val();
})

Upvotes: 0

Related Questions