adding values to dropdown lists in javascript

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

    #1

    adding values to dropdown lists in javascript

    I have searched high and low and am posting this question in the hope
    that someone out there on the net can help me out. I am using a merge
    of ASP and JavaScript.

    My goal is to create 2 drop down lists. Both are populated from a
    database using ASP, BUT the contents of the second list are dependant
    on the selection of the first list. When the user selects the state in
    the first list, then only the cities which reside in that state are
    displayed in the second dropdown list.

    I have this working to a point using the JavaScript command:[color=blue]
    > document.frmReg istration.cityN ame.options[0] = new Option("Perth") ;[/color]

    The problem with this is that although it adds a display value, each
    state has a stateID, which also needs to be included. In essence, the
    line of code above, does this:[color=blue]
    > <option>Perth </option>[/color]

    What I need it to do is this:[color=blue]
    > <option value="1">Perth </option>[/color]

    How do I get the 'value' field to insert using javascript ?!?!?!?!

    If anyone here knows, please PLEASE let me know... otherwise point me
    in the right direction.

    Thanks
  • kaeli

    #2
    Re: adding values to dropdown lists in javascript

    In article <53203c78.04090 80255.313b84f7@ posting.google. com>,
    evandelagrammat icas@hotmail.co m enlightened us with...[color=blue]
    >
    > My goal is to create 2 drop down lists. Both are populated from a
    > database using ASP, BUT the contents of the second list are dependant
    > on the selection of the first list. When the user selects the state in
    > the first list, then only the cities which reside in that state are
    > displayed in the second dropdown list.
    >[/color]

    Does this help?



    --
    --
    ~kaeli~
    The best part of having kids is giving them back to their
    parents.



    Comment

    • bruce

      #3
      Re: adding values to dropdown lists in javascript

      evandelagrammat icas@hotmail.co m (Evan) wrote in message news:<53203c78. 0409080255.313b 84f7@posting.go ogle.com>...[color=blue]
      > I have searched high and low and am posting this question in the hope
      > that someone out there on the net can help me out. I am using a merge
      > of ASP and JavaScript.
      >
      > My goal is to create 2 drop down lists. Both are populated from a
      > database using ASP, BUT the contents of the second list are dependant
      > on the selection of the first list. When the user selects the state in
      > the first list, then only the cities which reside in that state are
      > displayed in the second dropdown list.
      >
      > I have this working to a point using the JavaScript command:[color=green]
      > > document.frmReg istration.cityN ame.options[0] = new Option("Perth") ;[/color]
      >
      > The problem with this is that although it adds a display value, each
      > state has a stateID, which also needs to be included. In essence, the
      > line of code above, does this:[color=green]
      > > <option>Perth </option>[/color]
      >
      > What I need it to do is this:[color=green]
      > > <option value="1">Perth </option>[/color]
      >
      > How do I get the 'value' field to insert using javascript ?!?!?!?!
      >
      > If anyone here knows, please PLEASE let me know... otherwise point me
      > in the right direction.
      >
      > Thanks[/color]

      My work is all in IE, so I can only vouch for IE. I think that
      document.frmReg istration.cityN ame.options[0].value="your
      value"
      seems to work for me.

      You get "your value" from the other select box in the usual way
      (document.frmRe gistration.othe rSelectBox.opti ons[document.frmReg istratin.otherS electBox.select edIndex].value.

      Comment

      • Bart Van der Donck

        #4
        Re: adding values to dropdown lists in javascript

        Evan wrote:
        [color=blue]
        > My goal is to create 2 drop down lists. Both are populated from a
        > database using ASP, BUT the contents of the second list are dependant
        > on the selection of the first list. When the user selects the state in
        > the first list, then only the cities which reside in that state are
        > displayed in the second dropdown list.
        > I have this working to a point using the JavaScript command:
        > document.frmReg istration.cityN ame.options[0] = new Option("Perth") ;
        > The problem with this is that although it adds a display value, each
        > state has a stateID, which also needs to be included. In essence, the
        > line of code above, does this:
        > <option>Perth </option>
        > What I need it to do is this:
        > <option value="1">Perth </option>[/color]

        Taking your example as a starting point:

        ------
        document.frmReg istration.cityN ame.options['0'] = new
        Option("Perth", "1")
        ------

        However, you 'll probably need to rewrite that if you want 'cityName'
        to allow more than 1 city in the field. You could do someting like:

        ------
        document.frmReg istration.cityN ame.options[document.frmReg istration.cityN ame.length]
        = new Option("Perth", "1");
        document.frmReg istration.cityN ame.options[document.frmReg istration.cityN ame.length]
        = new Option("Sydney" ,"2");
        ------

        Now, you would also need to reset 'cityName' before populating it with
        the new content:

        ------
        while (document.frmRe gistration.city Name.options.le ngth)
        document.frmReg istration.cityN ame.options[0] = null;
        ------

        Hope this helps
        Bart

        Comment

        Working...