1
 function changeDay(monthSelect){ dayVal = day.selected.value; //secili ayi al optVal = monthSelect.selected.value; if(optVal=='Şubat' && dayVal > 29){ modifyDay(29); } } 

I get this error message for the line dayVal = day.selected.value:

Uncaught TypeError: Cannot read property 'value' of undefined

how can i reach the selected option's value?

1
  • I would recommend looking at jQuery, it will make tasks like these much simpler and more organized. Commented Jun 17, 2011 at 13:15

2 Answers 2

2

You can use the "selectedIndex" property of the select element, and the "options" array:

var val = monthSelect.options[monthSelect.selectedIndex].value; 

The option itself is what you get from the array:

var theOption = monthSelect.options[monthSelect.selectedIndex]; 

That's the only safe way to do it if you're dealing with older versions of IE (maybe all versions; I stopped experimenting on this a long time ago :-), but some browsers do make the "value" attribute of the select element track the selected option value appropriately.

Sign up to request clarification or add additional context in comments.

4 Comments

monthSelect.value does the job too and is safe to use.
@roberkules well that may be true, but my reptile brain remembers that it didn't work in IE6.
... or maybe it was old versions of Mozilla (like, pre-Firefox Mozilla).
Just checked it before with IETester, did work. Anyways, time to let go :)
0

The simplest way to get the value:

day.value 

And it works in all browsers, even IE 6

demo: http://jsfiddle.net/roberkules/8xwhG/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.