Solved

Getting value of selected option in Java Script

Posted on 2007-12-04
6
774 Views
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

0
Comment
Question by:Mr_Splash
6 Comments
 
LVL 36

Accepted Solution

by:
Zyloch earned 80 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) {
    alert(sel.options[sel.selectedIndex]);
}
0
 
LVL 3

Author Comment

by:Mr_Splash
ID: 20406887
Thanks Zyloch,

But I'm now getting [object HTMLOptionElement]

Implemented exactly as you specified.
0
 
LVL 19

Expert Comment

by:erikTsomik
ID: 20406901
try this
function updateMonth(sel) {
    alert(sel.options[sel.selectedIndex].value);
}
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 45 total points
ID: 20406905
Or simplified:

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

function updateMonth(theSel){
  alert(theSel.value);
}

0
 
LVL 36

Expert Comment

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

Expert Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now