Welcome to WebmasterWorld Guest from 54.91.4.56

Forum Moderators: open

How to change select option with JavaScript in select array

     
5:38 pm on May 12, 2019 (gmt 0)

Junior Member

10+ Year Member Top Contributors Of The Month

joined:May 2, 2004
posts: 86
votes: 0


This is what I have (that works)

<form name="testform">
<select name="firstvals" onChange="update(this.selectedIndex)">
<option selected>pick a number</option>
<option value="1">one</option>
<option value="2">two</option>
</select>
<select name="secondvals">
</select>
</form>

<script>
var secondvals=new Array();
secondvals[1]=["pick a second number|0","two_1|1","two_2|2"];
secondvals[2]=["pick a second number|0","two_a|1","two_b|2"];

function update(selectedOption){
var secondValsList=document.testform.secondvals;
secondValsList.options.length=0;
if(selectedOption>0){
for (i=0; i<secondvals[selectedOption].length; i++){
secondValsList.options[secondValsList.options.length]=new Option(secondvals[selectedOption][i].split("|")[0], secondvals[selectedOption][i].split("|")[1]);
}
}
}
</script>

Can anyone give me guidance on how I expand this to deal with multiple select[] eg:

<form name="testform">
<select name="firstvals[]" onChange="update(this.selectedIndex)">
<option selected>pick a number</option>
<option value="1">one</option>
<option value="2">two</option>
</select>
<select name="secondvals[]">
</select>
<select name="firstvals[]" onChange="update(this.selectedIndex)">
<option selected>pick a number</option>
<option value="1">one</option>
<option value="2">two</option>
</select>
<select name="secondvals[]">
</select>
</form>

Thank you
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members