Reputation:
So i know there have been many posts about this and ive read them, but the solutions recommended in this previous posts, dont work.
I have a function that dynamically creates me a drop down menu. Here it is:
function date(start, end) {
output ="";
for(i=start;i<=end;i++)
{
output += "<option value='"+i+"'>"+i+"</option>";
}
document.write(output);
}
And i call it like this:
Month:
<select name="mm" id="mm" disabled onchange="activateDate();">
<script type="text/javascript">
date(1,12);
</script>
</select>
Note: activateDay just changes the attribute disabled in the next dropdown menu, because you should first select a year, then a month, then a day.
and when i try to get the selected value of the month so that i can give a month a number of days (31 for january, 28 for february)like this :
var a = document.getElementById("mm");
var month = a.options[a.selectedIndex].value;
And if i use this vars for a switch which looks for the month and returns the number of the days, then i give the number of days to my date()
i get an empty drop down menu for my dates. The Javascript console in chrome says this:
Uncaught TypeError: Cannot read property 'options' of null
And points me to this line:
var month = a.options[a.selectedIndex].value;
So what am i doing wrong?
Upvotes: 0
Views: 1522
Reputation: 288680
The error
Cannot read property 'options' of null
means that a
is null in a.options[a.selectedIndex].value
.
Since you have an element with id="mm"
, check if:
window.document
document.getElementById
id
var a = document.getElementById("mm")
before loading the select to the DOMProbably its the last one, try running your code at onload
event, or place your script just before closing </body>
.
Upvotes: 1
Reputation: 1104
try to use this code
var month = a.options[a.selectedIndex-1].value;
Upvotes: 0