Reputation:
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
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
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