Firefox, problems with dynamically filling a select-list

S

Sjaakie

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

<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>
 
R

Random

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.


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

<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>
 
S

Sjaakie

Random said:
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.
 
R

Random

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>
 
S

Sjaakie

This indeed did the trick, FF doesn't allow non-0-based option lists.
Thanks for your help.

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>


never


No effect...




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

Firefox seems to act very differently compared to IE.
 
M

Michael Winter

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:

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.
 
R

RobG

Sjaakie said:
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

<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.
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top