-2

I am using the below code for select array in html

<select name="selection[1]">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>
<select name="selection[2]">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>

How do i retrieve the values from this select in javascript

  • 4
    Possible duplicate of [Get selected value in dropdown list using JavaScript?](http://stackoverflow.com/questions/1085801/get-selected-value-in-dropdown-list-using-javascript) – prasanth May 19 '17 at 11:40
  • 3
    Which select? Which values? And more importantly, what codes have you tried so far? – Ahs N May 19 '17 at 11:41

4 Answers4

1

You can use jQuery Attribute Starts With Selector [name^="value"]:

$('[name^="selection"]').each(function() {
  console.log($(this).attr('name'));
  console.log($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select name="selection[1]">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>
<select name="selection[2]">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
0

You can give id for each dropdown and get value like this:

<select name="selection[1]" id="selection1">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
</select>
<select name="selection[2]" id="selection2">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
</select>
<script>
var e = document.getElementById("selection1");
var strUser = e.options[e.selectedIndex].value;
console.log(strUser);
</script>
Jack
  • 24
  • 3
-1

First, you have to add id="selection[1]" and id="selection[2]" in the select control respectively, then you can call this function to get the selected value and text.

<script>
function Getdropdown1()
{
var e = document.getElementById("selection[1]");
var selectedValue1 = e.options[e.selectedIndex].value;
var selectedText1 = e.options[e.selectedIndex].text;
}
</script>

<script>
function Getdropdown2()
{
var e = document.getElementById("selection[2]");
var selectedValue2 = e.options[e.selectedIndex].value;
var selectedText2 = e.options[e.selectedIndex].text;
}
</script>
Azhar
  • 107
  • 12
-2

Try :

document.getElementsByName('selection[1]')[0].value
document.getElementsByName('selection[2]')[0].value
Deepak
  • 850
  • 5
  • 13