468,272 Members | 2,051 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

Firefox, problems with dynamically filling a select-list

Hi there,
I'm trying to get this working with Firefox. Can you point out what's
wrong? No problems with IE, Firefox doens't fill selMonth...

TIA
the script... >>>


<select name="selDay" id="selDay" style="width:50;" ></select> -
<select name="selMonth" id="selMonth" style="width:50;"></select> -
<select name="selYear" id="selYear" style="width:75;"></select>

<script language="JavaScript">

function initDateSelect() {
for (x=1; x<32; x++) {
document.getElementById('selDay').options[x-1] = new Option(x,x); }
for (x=1; x<13; x++) {
document.getElementById('selMonth').options[x] = new Option(x,x); }
for (x=1920; x<2020; x++) {
document.getElementById('selYear').options[x-1920] = new Option(x,x); }
}

initDateSelect();
</script>
Jul 23 '05 #1
6 2031
The problem might be that you're starting at 1 instead of 0. I've never
tried making .options a sparse array, so not sure how well it would
handle that.

Try
for (x=0; x<12; x++) {

I generally prefer something more like:

mySelectElement.add( new Option( strText, strValue ) );

----

Thought I'd also note that it's usually easier on the user to give them
a box to put the number in and then JS validate that number.

Sjaakie wrote:
Hi there,
I'm trying to get this working with Firefox. Can you point out what's wrong? No problems with IE, Firefox doens't fill selMonth...

TIA
>>> the script... >>>

<select name="selDay" id="selDay" style="width:50;" ></select> -
<select name="selMonth" id="selMonth" style="width:50;"></select> -
<select name="selYear" id="selYear" style="width:75;"></select>

<script language="JavaScript">

function initDateSelect() {
for (x=1; x<32; x++) {
document.getElementById('selDay').options[x-1] = new Option(x,x); }
for (x=1; x<13; x++) {
document.getElementById('selMonth').options[x] = new Option(x,x); }
for (x=1920; x<2020; x++) {
document.getElementById('selYear').options[x-1920] = new Option(x,x);

} }

initDateSelect();
</script>


Jul 23 '05 #2
Random wrote:
The problem might be that you're starting at 1 instead of 0. I've never
tried making .options a sparse array, so not sure how well it would
handle that.

Try
for (x=0; x<12; x++) {
No effect...

I generally prefer something more like:

mySelectElement.add( new Option( strText, strValue ) );


I tried
document.getElementById("selDay").add( new Option( "test","test" ) );
no effect at all.

Firefox seems to act very differently compared to IE.
Jul 23 '05 #3
for( x = 1; x < 13; x++ )
document.getElementById( 'selMonth' ).options[ x - 1 ] =
new Option( x, x );

Worked fine for me. FF apparently doesn't like making sparse arrays out
of .options lists, and I don't blame it.

Start at 0 for indices in .options.
My mistake on .add(), by the way. That's IE-only, so couldn't possibly
have fixed your problem.
Literally, here is the code I used:
<html><body><form>

<select name="selDay" id="selDay" style="width:50;" ></select> -
<select name="selMonth" id="selMonth" style="width:50;"></select> -
<select name="selYear" id="selYear" style="width:75;"></select>
<script language="JavaScript">

function initDateSelect( ) {
for( x = 1; x < 32; x++ )
document.getElementById( 'selDay' ).options[ x - 1 ] =
new Option( x, x );

for( x = 1; x < 13; x++ )
document.getElementById( 'selMonth' ).options[ x - 1 ] =
new Option( x, x );

for( x = 1920; x < 2020; x++ )
document.getElementById( 'selYear' ).options[ x - 1920 ] =
new Option( x, x );

}
initDateSelect();

</script>
</form></body></html>

Sjaakie wrote:
Random wrote:
The problem might be that you're starting at 1 instead of 0. I've never tried making .options a sparse array, so not sure how well it would
handle that.

Try
for (x=0; x<12; x++) {


No effect...

I generally prefer something more like:

mySelectElement.add( new Option( strText, strValue ) );


I tried
document.getElementById("selDay").add( new Option( "test","test" ) );
no effect at all.

Firefox seems to act very differently compared to IE.


Jul 23 '05 #4
This indeed did the trick, FF doesn't allow non-0-based option lists.
Thanks for your help.
Random wrote:
for( x = 1; x < 13; x++ )
document.getElementById( 'selMonth' ).options[ x - 1 ] =
new Option( x, x );

Worked fine for me. FF apparently doesn't like making sparse arrays out
of .options lists, and I don't blame it.

Start at 0 for indices in .options.
My mistake on .add(), by the way. That's IE-only, so couldn't possibly
have fixed your problem.
Literally, here is the code I used:
<html><body><form>

<select name="selDay" id="selDay" style="width:50;" ></select> -
<select name="selMonth" id="selMonth" style="width:50;"></select> -
<select name="selYear" id="selYear" style="width:75;"></select>
<script language="JavaScript">

function initDateSelect( ) {
for( x = 1; x < 32; x++ )
document.getElementById( 'selDay' ).options[ x - 1 ] =
new Option( x, x );

for( x = 1; x < 13; x++ )
document.getElementById( 'selMonth' ).options[ x - 1 ] =
new Option( x, x );

for( x = 1920; x < 2020; x++ )
document.getElementById( 'selYear' ).options[ x - 1920 ] =
new Option( x, x );

}
initDateSelect();

</script>
</form></body></html>

Sjaakie wrote:
Random wrote:
The problem might be that you're starting at 1 instead of 0. I've
never
tried making .options a sparse array, so not sure how well it would
handle that.

Try
for (x=0; x<12; x++) {


No effect...
I generally prefer something more like:

mySelectElement.add( new Option( strText, strValue ) );


I tried
document.getElementById("selDay").add( new Option( "test","test" ) );
no effect at all.

Firefox seems to act very differently compared to IE.

Jul 23 '05 #5
On 23/05/2005 12:15, Random wrote:

[snip]
My mistake on .add(), by the way. That's IE-only, so couldn't possibly
have fixed your problem.
No it isn't. The add method is defined by the W3C, but Microsoft have an
entirely incompatible implementation. The only way to use it reliably[1]
is to use a try/catch statement, but that will cause problems if a user
agent implements the add method, but not an exception mechanism.

[snip]
<select [...] style="width:50;">
With CSS, all non-zero length values must be accompanied by a unit
specifier.

[snip]
<script language="JavaScript">
The language attribute is deprecated. Use the (required) type attribute
instead:

<script type="text/javascript">
function initDateSelect( ) {
for( x = 1; x < 32; x++ )


If a variable has no business being global, ensure you declare it local.

function initDateSelect() {
var x;

/* ... */
}

[snip]

Mike
[1] I've experimented with other way that infers the expected
arguments based on the length property of the method, but
that may not be reliable.
Please don't use tabs when posting code. Use spaces - preferably two -
when indenting instead.

You've been instructed on how to post properly though Google Groups.
Please do so.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
Sjaakie wrote:
Hi there,
I'm trying to get this working with Firefox. Can you point out what's
wrong? No problems with IE, Firefox doens't fill selMonth...

TIA
>>> the script... >>>

<select name="selDay" id="selDay" style="width:50;" ></select> -
<select name="selMonth" id="selMonth" style="width:50;"></select> -
<select name="selYear" id="selYear" style="width:75;"></select>

<script language="JavaScript">

function initDateSelect() {
for (x=1; x<32; x++) {
document.getElementById('selDay').options[x-1] = new Option(x,x); }


Repeatedly using getElementById is likely slow, use a local variable
to hold a reference to the options and add to that.
for (x=1; x<13; x++) {
document.getElementById('selMonth').options[x] = new Option(x,x); }
Your first month option will be index 1, that seems to cause Firefox
to not display the options:

document.getElementById('selMonth').options[x-1] = new Option(x,x);

for (x=1920; x<2020; x++) {
document.getElementById('selYear').options[x-1920] = new Option(x,x); }
}

initDateSelect();
</script>


Here's another version that may run a bit faster and avoids
getElementById by using the forms collection (and therefore should
have wider browser support):

<form name="dForm" action="">
<select name="selDay" style="width: 3em;"></select> -
<select name="selMonth" style="width: 3em;"></select> -
<select name="selYear" style="width: 5em;"></select>
</form>
<script type="text/javascript">

var x = 0;
var opt = document.dForm.selDay.options;
while ( x++ < 31 ) { opt[x-1] = new Option(x,x); }

x = 0;
opt = document.dForm.selMonth.options;
while ( x++ < 12 ) { opt[x-1] = new Option(x,x); }

x = 0;
opt = document.dForm.selYear.options;
while ( x++ < 100 ) { opt[x-1] = new Option(x+1919,x+1919); }

</script>
Using 'em' as the unit for width means the size of the options will
scale to whatever font size the user's browser is set to. Using
script-generated form elements may cause accessibility issues for
some users and those without JavaScript will not see any options.
--
Rob
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by lkrubner | last post: by
3 posts views Thread by crjunk | last post: by
2 posts views Thread by tshad | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.