Firefox, problems with dynamically filling a select-list

Discussion in 'Javascript' started by Sjaakie, May 23, 2005.

  1. Sjaakie

    Sjaakie Guest

    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>
     
    Sjaakie, May 23, 2005
    #1
    1. Advertising

  2. Sjaakie

    Random Guest

    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>
     
    Random, May 23, 2005
    #2
    1. Advertising

  3. Sjaakie

    Sjaakie Guest

    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.
     
    Sjaakie, May 23, 2005
    #3
  4. Sjaakie

    Random Guest

    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.
     
    Random, May 23, 2005
    #4
  5. Sjaakie

    Sjaakie Guest

    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.

    >
     
    Sjaakie, May 23, 2005
    #5
  6. 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.
     
    Michael Winter, May 23, 2005
    #6
  7. Sjaakie

    RobG Guest

    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
     
    RobG, May 23, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. brett
    Replies:
    1
    Views:
    491
    J├╝rgen Exner
    Feb 21, 2005
  2. J. Muenchbourg
    Replies:
    1
    Views:
    168
    Dr John Stockton
    Jul 29, 2003
  3. david
    Replies:
    3
    Views:
    113
    Lasse Reichstein Nielsen
    Nov 13, 2003
  4. Mohsen Pahlevanzadeh
    Replies:
    0
    Views:
    137
    Mohsen Pahlevanzadeh
    Sep 9, 2013
  5. MRAB
    Replies:
    0
    Views:
    129
Loading...

Share This Page