How to reference one combo box from another combo box

Discussion in 'Javascript' started by SM, Apr 28, 2007.

  1. SM

    SM Guest

    Hello,
    I have an HTML file that contains a form with 2 selection list (combo
    box). The first combobox 'onchange' function, addColor(),adds a new
    color to the second combobox. I have half of the function build, but
    im missing the how to reference one combobox from another combobox.
    I've tried using the getElementById with no success.



    This is how the form with the 2 combobox looks like:

    <form>
    <select name="cars" size="1" onchange="addColor(???)">
    <option value="Volvo">Volvo</option>
    <option value="Mercedes">Mercedes</option>
    <option value="BMW" selected="selected">BMW</option>
    <option value="Audi">Audi</option>

    </select>
    <select name='colors' size='1'>
    <option value='red' selected="selected">red</option>
    <option value='blueblue'>blue</option>
    <option value='black'>black</option>
    <option value='yellow'>yellow</option>
    </select>
    </form>



    This is the 'onchange' function that adds a new color to the second
    combo box
    function addColor(???)
    {
    ......

    for (var i=0; i<album.length; i++) //loop through the array
    {
    var newOpt = document.createElement('option');
    newOpt.setAttribute('value', i);
    if (!i) { newOpt.setAttribute('selected', 'selected'); } //if
    first element, add extra attribute to <newOpt>

    ???.appendChild(newOpt); //??? doesnt work, coz reference is
    wrong

    newOptValue = document.createTextNode(album(i));
    newOpt.appendChild(newOptValue)
    }
    }


    How do i achieve this?

    Thanks
    Marco
     
    SM, Apr 28, 2007
    #1
    1. Advertising

  2. SM wrote:
    > <form>
    > <select name="cars" size="1" onchange="addColor(this.form.colors)">...
    > </select>
    > <select name='colors' size='1'>...
    > </select>
    > </form>
    >


    > function addColor(sel)
    > {
    > ......
    >
    > for (var i=0; i<album.length; i++) //loop through the array
    > {
    > var newOpt = document.createElement('option');
    > newOpt.setAttribute('value', i);
    > if (!i) { newOpt.setAttribute('selected', 'selected'); } //if
    > first element, add extra attribute to <newOpt>
    >
    > sel.appendChild(newOpt); //??? doesnt work, coz reference is
    > wrong
    >
    > newOptValue = document.createTextNode(album(i));
    > newOpt.appendChild(newOptValue)
    > }
    > }



    ^^ code
     
    scripts.contact, Apr 28, 2007
    #2
    1. Advertising

  3. SM

    SM Guest

    On Apr 28, 2:06 am, "scripts.contact" <>
    wrote:
    > SM wrote:
    > > <form>
    > > <select name="cars" size="1" onchange="addColor(this.form.colors)">...
    > > </select>
    > > <select name='colors' size='1'>...
    > > </select>
    > > </form>

    >
    > > function addColor(sel)
    > > {
    > > ......

    >
    > > for (var i=0; i<album.length; i++) //loop through the array
    > > {
    > > var newOpt = document.createElement('option');
    > > newOpt.setAttribute('value', i);
    > > if (!i) { newOpt.setAttribute('selected', 'selected'); } //if
    > > first element, add extra attribute to <newOpt>

    >
    > > sel.appendChild(newOpt); //??? doesnt work, coz reference is
    > > wrong

    >
    > > newOptValue = document.createTextNode(album(i));
    > > newOpt.appendChild(newOptValue)
    > > }
    > > }

    >
    > ^^ code


    Thanks, works like a charm

    Marco
     
    SM, Apr 28, 2007
    #3
    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. =?Utf-8?B?QW5nZWw=?=

    COMBO BOX INSTEAD OF DROP DOWN BOX

    =?Utf-8?B?QW5nZWw=?=, Jun 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    628
    Ken Cox [Microsoft MVP]
    Jun 29, 2004
  2. SirPoonga
    Replies:
    2
    Views:
    799
    Ben Strackany
    Jan 7, 2005
  3. Robert Cohen
    Replies:
    3
    Views:
    291
    Andrew Durstewitz
    Jul 15, 2003
  4. Adrian
    Replies:
    1
    Views:
    235
    Bullschmidt
    Feb 18, 2004
  5. dthmtlgod

    Fill Combo Box based on another Combo Box

    dthmtlgod, Nov 16, 2005, in forum: ASP General
    Replies:
    1
    Views:
    605
    Bob Barrows [MVP]
    Nov 16, 2005
Loading...

Share This Page