Firefox, problems with dynamically filling a select-list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sjaakie

    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
    [color=blue][color=green][color=darkred]
    >>> the script... >>>[/color][/color][/color]

    <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="JavaS cript">

    function initDateSelect( ) {
    for (x=1; x<32; x++) {
    document.getEle mentById('selDa y').options[x-1] = new Option(x,x); }
    for (x=1; x<13; x++) {
    document.getEle mentById('selMo nth').options[x] = new Option(x,x); }
    for (x=1920; x<2020; x++) {
    document.getEle mentById('selYe ar').options[x-1920] = new Option(x,x); }
    }

    initDateSelect( );
    </script>
  • Random

    #2
    Re: Firefox, problems with dynamically filling a select-list

    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:[color=blue]
    > Hi there,
    > I'm trying to get this working with Firefox. Can you point out what's[/color]
    [color=blue]
    > wrong? No problems with IE, Firefox doens't fill selMonth...
    >
    > TIA
    >[color=green][color=darkred]
    > >>> the script... >>>[/color][/color]
    >
    > <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="JavaS cript">
    >
    > function initDateSelect( ) {
    > for (x=1; x<32; x++) {
    > document.getEle mentById('selDa y').options[x-1] = new Option(x,x); }
    > for (x=1; x<13; x++) {
    > document.getEle mentById('selMo nth').options[x] = new Option(x,x); }
    > for (x=1920; x<2020; x++) {
    > document.getEle mentById('selYe ar').options[x-1920] = new Option(x,x);[/color]
    }[color=blue]
    > }
    >
    > initDateSelect( );
    > </script>[/color]

    Comment

    • Sjaakie

      #3
      Re: Firefox, problems with dynamically filling a select-list

      Random wrote:[color=blue]
      > 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++) {[/color]

      No effect...

      [color=blue]
      > I generally prefer something more like:
      >
      > mySelectElement .add( new Option( strText, strValue ) );[/color]

      I tried
      document.getEle mentById("selDa y").add( new Option( "test","tes t" ) );
      no effect at all.

      Firefox seems to act very differently compared to IE.

      Comment

      • Random

        #4
        Re: Firefox, problems with dynamically filling a select-list

        for( x = 1; x < 13; x++ )
        document.getEle mentById( '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><fo rm>

        <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="JavaS cript">

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

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

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

        }


        initDateSelect( );

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



        Sjaakie wrote:[color=blue]
        > Random wrote:[color=green]
        > > The problem might be that you're starting at 1 instead of 0. I've[/color][/color]
        never[color=blue][color=green]
        > > tried making .options a sparse array, so not sure how well it would
        > > handle that.
        > >
        > > Try
        > > for (x=0; x<12; x++) {[/color]
        >
        > No effect...
        >
        >[color=green]
        > > I generally prefer something more like:
        > >
        > > mySelectElement .add( new Option( strText, strValue ) );[/color]
        >
        > I tried
        > document.getEle mentById("selDa y").add( new Option( "test","tes t" ) );
        > no effect at all.
        >
        > Firefox seems to act very differently compared to IE.[/color]

        Comment

        • Sjaakie

          #5
          Re: Firefox, problems with dynamically filling a select-list

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


          Random wrote:[color=blue]
          > for( x = 1; x < 13; x++ )
          > document.getEle mentById( '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><fo rm>
          >
          > <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="JavaS cript">
          >
          > function initDateSelect( ) {
          > for( x = 1; x < 32; x++ )
          > document.getEle mentById( 'selDay' ).options[ x - 1 ] =
          > new Option( x, x );
          >
          > for( x = 1; x < 13; x++ )
          > document.getEle mentById( 'selMonth' ).options[ x - 1 ] =
          > new Option( x, x );
          >
          > for( x = 1920; x < 2020; x++ )
          > document.getEle mentById( 'selYear' ).options[ x - 1920 ] =
          > new Option( x, x );
          >
          > }
          >
          >
          > initDateSelect( );
          >
          > </script>
          > </form></body></html>
          >
          >
          >
          > Sjaakie wrote:
          >[color=green]
          >>Random wrote:
          >>[color=darkred]
          >>>The problem might be that you're starting at 1 instead of 0. I've[/color][/color]
          >
          > never
          >[color=green][color=darkred]
          >>>tried making .options a sparse array, so not sure how well it would
          >>>handle that.
          >>>
          >>>Try
          >>>for (x=0; x<12; x++) {[/color]
          >>
          >>No effect...
          >>
          >>
          >>[color=darkred]
          >>>I generally prefer something more like:
          >>>
          >>>mySelectElem ent.add( new Option( strText, strValue ) );[/color]
          >>
          >>I tried
          >>document.getE lementById("sel Day").add( new Option( "test","tes t" ) );
          >>no effect at all.
          >>
          >>Firefox seems to act very differently compared to IE.[/color]
          >[/color]

          Comment

          • Michael Winter

            #6
            Re: Firefox, problems with dynamically filling a select-list

            On 23/05/2005 12:15, Random wrote:

            [snip]
            [color=blue]
            > My mistake on .add(), by the way. That's IE-only, so couldn't possibly
            > have fixed your problem.[/color]

            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]
            [color=blue]
            > <select [...] style="width:50 ;">[/color]

            With CSS, all non-zero length values must be accompanied by a unit
            specifier.

            [snip]
            [color=blue]
            > <script language="JavaS cript">[/color]

            The language attribute is deprecated. Use the (required) type attribute
            instead:

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

            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.

            Comment

            • RobG

              #7
              Re: Firefox, problems with dynamically filling a select-list

              Sjaakie wrote:[color=blue]
              > 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
              >[color=green][color=darkred]
              > >>> the script... >>>[/color][/color]
              >
              > <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="JavaS cript">
              >
              > function initDateSelect( ) {
              > for (x=1; x<32; x++) {
              > document.getEle mentById('selDa y').options[x-1] = new Option(x,x); }[/color]

              Repeatedly using getElementById is likely slow, use a local variable
              to hold a reference to the options and add to that.
              [color=blue]
              > for (x=1; x<13; x++) {
              > document.getEle mentById('selMo nth').options[x] = new Option(x,x); }[/color]

              Your first month option will be index 1, that seems to cause Firefox
              to not display the options:

              document.getEle mentById('selMo nth').options[x-1] = new Option(x,x);

              [color=blue]
              > for (x=1920; x<2020; x++) {
              > document.getEle mentById('selYe ar').options[x-1920] = new Option(x,x); }
              > }
              >
              > initDateSelect( );
              > </script>[/color]

              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.option s;
              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

              Comment

              Working...