Brendan Gooden Brendan Gooden - 5 months ago 10
HTML Question

Javascript get Selected Value returning 'undefined'

I have an ASP.NET website, with the following code in an

.aspx
page.

<form action="#" id="demoForm" class="demoForm">
<select id="SelectEquipmentDropdown">
<option value="1">1</option>
<option value="2">2</option>
</select>
</form>


When this renders in HTML, I get the following

<form action="#" id="demoForm" class="demoForm">
<select id="SelectEquipmentDropdown">
<option value="1">1</option>
<option value="2">2</option>
</select>
</form>


And on my HTML page, I see this

enter image description here

When I use this javascript to return the
selectedValue
of the option, I get the value
undefined
returning
. Can someone please tell me what I'm doing wrong???

var e = document.getElementById("SelectEquipmentDropdown");
var selectedEquipmentDropdown = e.options[e.selectedIndex].Value;

Answer

.Value should be.value see fiddle https://jsfiddle.net/Lddyn573/9/

<form action="#" id="demoForm" class="demoForm">
  <select id="SelectEquipmentDropdown" onchange="myFunction()">
    <option value="1">1</option>
    <option value="2">2</option>
  </select>
</form>
<script>
function myFunction(){
var e = document.getElementById("SelectEquipmentDropdown");
var selectedEquipmentDropdown = e.options[e.selectedIndex].value;//change it here

alert(selectedEquipmentDropdown);
}
</script>