Mr_Splash
asked on
Getting value of selected option in Java Script
I'm trying to get the value of the selected option when a drop down is changed like so;
<select name="months" id="months" onchange="updateMonth();">
However when I change the drop down, the alert says "Undefined" where it's meant to say "January"
Can someone tell me where I'm going wrong?
<select name="months" id="months" onchange="updateMonth();">
However when I change the drop down, the alert says "Undefined" where it's meant to say "January"
Can someone tell me where I'm going wrong?
function updateMonth() {
var opIndex = document.book.months.selectedIndex;
var opValue = new Array(document.book.months.Options);
alert (opValue[opIndex]);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try this
function updateMonth(sel) {
alert(sel.options[sel.sele ctedIndex] .value);
}
function updateMonth(sel) {
alert(sel.options[sel.sele
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That's right. I can't believe I forgot the value attribute, thanks for the save.
that is fine , but got no points anyway.
ASKER
But I'm now getting [object HTMLOptionElement]
Implemented exactly as you specified.