2

I have an array of select field and want to get the value of selected option using jQuery.

Select field is like

<select name="a[]" onchange="getValue(this)">
  <option value="11">11</option>
  <option value="12">12</option>
</select>
<select name="a[]" onchange="getValue(this)">
  <option value="21">21</option>
  <option value="22">22</option>
</select>

and my javascript code is

function getValue(ele) {
  alert(ele.val());
}

But it is not working.

Ben Harrison
  • 2,121
  • 4
  • 24
  • 40
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285

3 Answers3

4
<select name="a[]">
  <option value="11">11</option>
  <option value="12">12</option>
</select>
<select name="a[]">
  <option value="21">21</option>
  <option value="22">22</option>
</select>

javascript

$("select").on("change", function (event){
   alert($(this).val());
})

if you dont want to select all the select input list then you can do this also

$("select[name='a\[\]']").on("change", function (event){
       alert($(this).val());
    })

this will only select only select list with name=a[]

atul
  • 552
  • 4
  • 16
2

The val() is a jQuery method which can't use with DOM object. To get value use value property of the element instead.

function getValue(ele) {
  alert(ele.value);
  // with jQuery it should be
  // alert($(ele).val());
}

function getValue(ele) {
  alert(ele.value);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="a[]" onchange="getValue(this)">
  <option value="11">11</option>
  <option value="12">12</option>
</select>
<select name="a[]" onchange="getValue(this)">
  <option value="21">21</option>
  <option value="22">22</option>
</select>

Since you are tagged jQuery in your question use change() event handler, Inside the change event callback this refers to the corresponding dom object.

// select element with the name  attribute equals 
// selector can be used and then bind event handler
$('[name="a[]"]').change(function() {
  // get the value
  alert(this.value)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="a[]">
  <option value="11">11</option>
  <option value="12">12</option>
</select>
<select name="a[]">
  <option value="21">21</option>
  <option value="22">22</option>
</select>
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
2

Try this :

$(document).ready(function(){

    $("select").on("change",function(){

        alert($(this).val());


    })
})

Final code :

<!DOCTYPE html>
<html lang="en">
<head>
</head>
    
    <body>
        
        <select name="a[]">
          <option value="11">11</option>
          <option value="12">12</option>
        </select>
        <select name="a[]">
          <option value="21">21</option>
          <option value="22">22</option>
        </select>
        
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script>
            
        $(document).ready(function(){

            $("select").on("change",function(){

                alert($(this).val());


            })
        })
    </script>
    </body>
</html>
Ehsan
  • 12,655
  • 3
  • 25
  • 44