Link to home
Start Free TrialLog in
Avatar of Mr_Splash
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?
function updateMonth() {
	var opIndex = document.book.months.selectedIndex;
	var opValue = new Array(document.book.months.Options);
	alert (opValue[opIndex]);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Zyloch
Zyloch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mr_Splash
Mr_Splash

ASKER

Thanks Zyloch,

But I'm now getting [object HTMLOptionElement]

Implemented exactly as you specified.
try this
function updateMonth(sel) {
    alert(sel.options[sel.selectedIndex].value);
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's right. I can't believe I forgot the value attribute, thanks for the save.
that is fine , but got no points anyway.