How to reference one combo box from another combo box

S

SM

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
 
S

scripts.contact

SM said:
<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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top