user1395001
user1395001

Reputation:

Get selected value from select tag

When I select a value from the option and click on a button, I want to fetch the selected value with javascript. What am I doing wrong? My value is always 1.

 <select id="aand_select">
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
    </select>

// javascript code

var e = document.getElementById("aand_select");
var quantity= e.options[e.selectedIndex].value;

Upvotes: 1

Views: 11249

Answers (2)

Caio Bianchi
Caio Bianchi

Reputation: 113

This apparently will do

var e = document.getElementById("aand_select");
e.addEventListener('change', function(){
  var quantity= e.options[e.selectedIndex].value;
  console.log(quantity);
},false);​​​​​​​​​​

Upvotes: 3

Dan Armstrong
Dan Armstrong

Reputation: 2076

Make sure that you are calling the javascript at the appropriate time. For example if you are calling it only when the page loads, the value will never change. Make sure you are calling it from an onClick() or some other event.

Upvotes: 1

Related Questions