Thank you knightEknight.
You are the best:)
Main Topics
Browse All TopicsHow to dinamicaly fill <select> tag with javascript.
eg.:
I have two buttons:
"Audi" "BMW"
and "Submit"
When I click "Audi" select tag should include:
<select name='cars'>
<option value='1'>A6</option>
<option value='2'>A4</option>
<option value='3'>A4</option>
</select>
And when I click "BMW" select tag should include:
<select name='cars'>
<option value='4'>316</option>
<option value='5'>320</option>
</select>
It should be something like this:
document.form1.cars.select
document.form1.cars.select
and so on...
Please help...
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: knightEknightPosted on 2003-07-14 at 08:34:43ID: 8918023
use .text instead of .innerText on OPTIONs.
) // pass in the current State index
tions; // points to selCity options array
Index].len gth; i<n; i++ ) ngth-1] = new Option(aryCityData[curStat eIndex][i] .text,aryC ityData[cu rStateInde x][i].valu e);
ngth-1].te xt = aryCityData[curStateIndex] [i].text; ngth-1].va lue = aryCityData[curStateIndex] [i].value;
lState.foc us();'>
s.selected Index);'> ion> >
ns[this.se lectedInde x].value)' >
Here is an example using cities and states:
<HTML>
<HEAD>
<TITLE>Dynamic SELECTs</title>
<SCRIPT language='JavaScript'>
<!-- //
// create js arrays to hold the data for selCity
var aryCityData = new Array();
aryCityData[0] = new Array();
aryCityData[0][0] = new Option('Select a State','0');
aryCityData[1] = new Array();
aryCityData[1][0] = new Option('Los Angeles','301');
aryCityData[1][1] = new Option('Oakland','304');
aryCityData[1][2] = new Option('Rio Linde','305');
aryCityData[1][3] = new Option('San Diego','302');
aryCityData[1][4] = new Option('San Fransisco','303');
aryCityData[2] = new Array();
aryCityData[2][0] = new Option('Miami','101');
aryCityData[2][1] = new Option('Orlando','102');
aryCityData[2][2] = new Option('Tampa','103');
aryCityData[3] = new Array();
aryCityData[3][0] = new Option('Abilene','201');
aryCityData[3][1] = new Option('Austin','202');
aryCityData[3][2] = new Option('Dallas','203');
aryCityData[3][3] = new Option('Houston','204');
function reloadCities(curStateIndex
{
if ( curStateIndex < 0 ) return; // no state selected, so do nothing
var aryCityOpts = document.myform.selCity.op
aryCityOpts.length=0; // first, clear the current City options ...
// then re-load the City options with values for the current State
for ( var i=0,n=aryCityData[curState
{
//// the line below no longer works in IE5 (it works in IE4 and NS4+).
// aryCityOpts[aryCityOpts.le
//// therefore, it now takes three lines to do the same thing:
aryCityOpts.length++; // add a new Option, then put data in it (below)
aryCityOpts[aryCityOpts.le
aryCityOpts[aryCityOpts.le
}
}
// -->
</script>
</head>
<BODY onLoad='document.myform.se
<FORM name='myform' method='post' action='whatever.asp' onSubmit='return(false);'>
State:
<SELECT name='selState' size='1' onChange='reloadCities(thi
<OPTION value='0'></option>
<OPTION value='30'>California</opt
<OPTION value='10'>Florida</option
<OPTION value='20'>Texas</option>
</select><P>
City:
<SELECT name='selCity' size='1' onChange='alert(this.optio
<OPTION value=''>Select a State</option>
<OPTION></option>
<OPTION></option>
<OPTION></option>
</select><P>
</form>
</body>
</html>