1

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>;
Carlitos Overflow
  • 609
  • 3
  • 13
  • 41

5 Answers5

4

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

Community
  • 1
  • 1
Alex Char
  • 32,879
  • 9
  • 49
  • 70
0
$('.promte').each(function(){
  // this.value;
  // OR
  // $(this).val();
})
Lee Wise
  • 902
  • 7
  • 16
0
var count = 0;
var array = [];

$('.promote').each(function(){
    array[count] = $(this).val();
    count++;
});
Xogle
  • 363
  • 3
  • 16
0
$(".promote").each(function() {
    var $selectBox = $(this);
    var selectedOption = $selectBox.find(":selected").val();
});

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

thiag0
  • 2,199
  • 5
  • 31
  • 51
0

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/

Manivannan
  • 3,074
  • 3
  • 21
  • 32