Using proper DOM with dynamic select arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news@celticbear.com

    #1

    Using proper DOM with dynamic select arrays

    Based on a condition, I want to add new options into a form's SELECT
    menu.

    This method has been working for Firefox, but not IE/Opera/Avant:
    document.form1. aem_from[0]=new Option("[ select ]","X");

    (Although the Firefox JavascriptConso le would of course tell me it's
    not good.)

    Trying to get it to work in every other browser than Firefox...how the
    heck to I format that using proper DOM?

    I've tried:
    document.getEle mentById('aem_f rom'[0])=new Option("[ select ]","X");
    and with various other placements of quotes and positioning of the
    array brackets, like:
    document.getEle mentById('aem_f rom')[0]=new Option("[ select ]","X");
    and:
    document.getEle mentById('aem_f rom[0]')=new Option("[ select ]","X");
    and it none of it works in any browser and the Console simply says I
    have an "invalid assignment left-hand side".

    I tried doing a search on the newsgroup...but I can't find anything
    quite like this. Maybe threads that are similar, but I didn't grok it
    enough to carry over the lesson given. My understanding of JavaScript
    is less "understand ing" and more "seething hatred of a necessary evil
    in my life." =)

    Thanks for any suggestions!
    Liam

  • RobB

    #2
    Re: Using proper DOM with dynamic select arrays

    n...@celticbear .com wrote:[color=blue]
    > Based on a condition, I want to add new options into a form's SELECT
    > menu.
    >
    > This method has been working for Firefox, but not IE/Opera/Avant:
    > document.form1. aem_from[0]=new Option("[ select ]","X");
    >
    > (Although the Firefox JavascriptConso le would of course tell me it's
    > not good.)
    >
    > Trying to get it to work in every other browser than Firefox...how[/color]
    the[color=blue]
    > heck to I format that using proper DOM?
    >
    > I've tried:
    > document.getEle mentById('aem_f rom'[0])=new Option("[ select ]","X");
    > and with various other placements of quotes and positioning of the
    > array brackets, like:
    > document.getEle mentById('aem_f rom')[0]=new Option("[ select ]","X");
    > and:
    > document.getEle mentById('aem_f rom[0]')=new Option("[ select ]","X");
    > and it none of it works in any browser and the Console simply says I
    > have an "invalid assignment left-hand side".
    >
    > I tried doing a search on the newsgroup...but I can't find anything
    > quite like this. Maybe threads that are similar, but I didn't grok it
    > enough to carry over the lesson given. My understanding of JavaScript
    > is less "understand ing" and more "seething hatred of a necessary evil
    > in my life." =)
    >
    > Thanks for any suggestions!
    > Liam[/color]

    document.getEle mentById() just gets you the element (object) with that
    id set - if it exists - or returns null. Start with that:

    document.getEle mentById('aem_f rom')

    Then reference the first Options[] slot:

    document.getEle mentById('aem_f rom').options[0]

    ....and add your new option:

    document.getEle mentById('aem_f rom').options[0]=new Option("[ select
    ]","X");

    As you can see - you did that already, at least the shorthand version
    (no reference to the options collection). Did you give the listbox an
    id as well as a name?

    Comment

    • news@celticbear.com

      #3
      Re: Using proper DOM with dynamic select arrays

      RobB wrote:
      ....[color=blue]
      > document.getEle mentById('aem_f rom').options[0]=new Option("[ select
      > ]","X");
      >
      > As you can see - you did that already, at least the shorthand version
      > (no reference to the options collection). Did you give the listbox an
      > id as well as a name?[/color]

      That did it. Amazing what just a period will do. =/

      Yeah, all the rest of my naming was fine, I just needed to know to put
      that period there. =P

      Thanks for the reply!!
      Liam

      Comment

      Working...