How to add <option> on Mac IE 5.0+

C

Cylix

My code works under MS IE6,
but not work on Firefox and Mac IE5.1.2
Mac IE5.1.2 response: 'options' is not an object

function to check leap year
Source code:
--------------------------------------------------------------------
function setStartDayOpt() {
var year = document.getElementById('startYear');
var month = document.getElementById('StartMonth');
var day = document.getElementById('startDay');
var i, j, m, y, temp;

m = month.options[month.selectedIndex].value;
y = year.options[year.selectedIndex].value

if (year && month && day) {
if (m==4 || m==6 || m==9 || m==11) {
i=30;
} else {
i=31;
}
if (m==2) {
if (y/4 == parseInt(y/4)) {
i = 29;
} else {
i = 28;
}
}
if (day.options.length == j) {
return false;
} else {
day.options.length = 0
}
for (j=0; j<i; j++) {
temp = new Option(j+1, j+1);
alert(temp);
day.options[j] = temp;
temp = null;
}
}
}
 
R

Richard Cornford

Cylix said:
My code works under MS IE6,
but not work on Firefox and Mac IE5.1.2
Mac IE5.1.2 response: 'options' is not an object

function to check leap year
Source code:
<snip>

If "startYear" is not assigned to the _ID_ attribute of the form
control (if it is the NAME of the control instead/only) then Windows IE
will retrieve a reference to the control using getElementById and most
other browsers will not (as they take the "Id" in getElementById as
meaning what it says).

Richard.
 
R

RobG

Cylix said:
My code works under MS IE6,
but not work on Firefox and Mac IE5.1.2
Mac IE5.1.2 response: 'options' is not an object

function to check leap year

It seems you are trying to put an appropriate number of days in a 'days'
option to match the year and month. Remember that browsers without
script should still be able to use your select elements.

There is a suggested script below, if scripting is not enabled it will
still work OK. Tested in IE 5.2, Safari and Firefox.

I've substituted a different 'daysInMonth' function, keep it if you like
(or not...). document.write is used to generate the initial days
options just for posting, use HTML in real life.


<html>
<head>
<title>date selector example</title>
<script type="text/javascript">

function daysInMonth(y, m)
{
m = +m + 1;
m = (m<10)? '0'+m : ''+m;
var x = new Date(y+'/'+m+'/'+'00');
return x.getDate();
}

function addDays()
{
var year = document.calendar.year;
var month = document.calendar.month;
var day = document.calendar.day;
var numDays = daysInMonth(year[year.selectedIndex].value,
month[month.selectedIndex].value);
day.options.length = 0;
for (var i=0; i<numDays; i++){
day.options = new Option(i+1, i+1);
}
}

</script>
</head>

<body>
<form name="calendar" action="">
<select id="year" onchange="addDays()">
<option value="2000">2000
<option value="2001">2001
<option value="2002">2002
<option value="2003">2003
<option value="2004">2004
<option value="2005">2005
</select>
<select id="month" onchange="addDays()">
<option value="1">January
<option value="2">February
<option value="3">March
<option value="4">April
<option value="5">May
<option value="6">June
</select>
<select id="day">
<script type="text/javascript">
// Used just to cut down on code, use HTML in production
var t = '';
var i = 0;
while(i < 31){
t += '<option value="'+ ++i +'">'+i;
}
document.write(t);
</script>
</select>
</form>
</body>
</html>
 
C

Cylix

You are so smart.
Actually, I still don't under start the behaviour.
After I change to the following in my script, my script also work fine:
var year = document.calendar.year;
var month = document.calendar.month;
var day = document.calendar.day;

Cylix said:
My code works under MS IE6,
but not work on Firefox and Mac IE5.1.2
Mac IE5.1.2 response: 'options' is not an object

function to check leap year

It seems you are trying to put an appropriate number of days in a 'days'
option to match the year and month. Remember that browsers without
script should still be able to use your select elements.

There is a suggested script below, if scripting is not enabled it will
still work OK. Tested in IE 5.2, Safari and Firefox.

I've substituted a different 'daysInMonth' function, keep it if you like
(or not...). document.write is used to generate the initial days
options just for posting, use HTML in real life.


<html>
<head>
<title>date selector example</title>
<script type="text/javascript">

function daysInMonth(y, m)
{
m = +m + 1;
m = (m<10)? '0'+m : ''+m;
var x = new Date(y+'/'+m+'/'+'00');
return x.getDate();
}

function addDays()
{
var year = document.calendar.year;
var month = document.calendar.month;
var day = document.calendar.day;
var numDays = daysInMonth(year[year.selectedIndex].value,
month[month.selectedIndex].value);
day.options.length = 0;
for (var i=0; i<numDays; i++){
day.options = new Option(i+1, i+1);
}
}

</script>
</head>

<body>
<form name="calendar" action="">
<select id="year" onchange="addDays()">
<option value="2000">2000
<option value="2001">2001
<option value="2002">2002
<option value="2003">2003
<option value="2004">2004
<option value="2005">2005
</select>
<select id="month" onchange="addDays()">
<option value="1">January
<option value="2">February
<option value="3">March
<option value="4">April
<option value="5">May
<option value="6">June
</select>
<select id="day">
<script type="text/javascript">
// Used just to cut down on code, use HTML in production
var t = '';
var i = 0;
while(i < 31){
t += '<option value="'+ ++i +'">'+i;
}
document.write(t);
</script>
</select>
</form>
</body>
</html>
 
R

RobG

Cylix said:
You are so smart.

Please don't top post in this group. Reply below quoted text, and trim
what is no longer relevant.
Actually, I still don't under start the behaviour.
After I change to the following in my script, my script also work fine:
var year = document.calendar.year;
var month = document.calendar.month;
var day = document.calendar.day;

Probably for exactly the reason that Richard noted - your elements had
'name' attributes but not 'id' attributes - IE tends to treat them the
same, so when using getElementById it will return an element with a
matching name attribute value. Other browsers treat name and id as
different attributes (because they are) and won't match name values
using getElementById.

The formal version of the above is:

var year = document.forms['calendar'].elements['year'];
var month = document.forms['calendar'].elements['month'];
var day = document.forms['calendar'].elements['day'];

which uses the document's forms collection, not its getElementById
method.

It is generally advisable to use the forms collection wherever possible
as it works consistently in pretty much every browser since Navigator 2
& IE 3.
 
D

Dr John Stockton

JRS: In article <448d48e9$0$7207$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Mon, 12 Jun 2006 20:58:39 remote, seen in
news:comp.lang.javascript said:
function daysInMonth(y, m)
{
m = +m + 1;
m = (m<10)? '0'+m : ''+m;
var x = new Date(y+'/'+m+'/'+'00');
return x.getDate();
}

Using new Date(Y, M, 0) seems simpler; and
new Date(Date.UTC(Y, M, 0)).getUTCDate() may be faster.

The OP's code is for the Julian calendar; ours is for the Gregorian.

The OP had :-
if (m==2) {
if (y/4 == parseInt(y/4)) {
i = 29;
} else {
i = 28;
}
}

but this is simpler :-
if (m==2) i = 28 + (y%4==0)

That way of using parseInt, I guess, comes from an old & bad book,
possibly via a low-grade lecturer.


However, I know little of Macs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top