[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Getting value of selected option in Java Script

Posted on 2007-12-04
Medium Priority
Last Modified: 2008-02-01
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

Question by:Mr_Splash
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 36

Accepted Solution

Zyloch earned 320 total points
ID: 20406815
Do not create a new array. Instead, do something like this:

<select name="months" id="months" onchange="updateMonth(this);">

Then, you can have

function updateMonth(sel) {

Author Comment

ID: 20406887
Thanks Zyloch,

But I'm now getting [object HTMLOptionElement]

Implemented exactly as you specified.
LVL 19

Expert Comment

ID: 20406901
try this
function updateMonth(sel) {
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 63

Assisted Solution

Zvonko earned 180 total points
ID: 20406905
Or simplified:

<select name="months" id="months" onchange="updateMonth(this);">

function updateMonth(theSel){

LVL 36

Expert Comment

ID: 20407015
That's right. I can't believe I forgot the value attribute, thanks for the save.
LVL 19

Expert Comment

ID: 20407053
that is fine , but got no points anyway.

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question