Anyway, I wrote up a quick and minimal javascript function to do all the work you want for you. It supports any level drop down boxes, so if you want to add a 4th drop down box, it should work just fine.
Basically to get it working all you have to do is set up your select options in the way described below, and then copy and paste the block of javascript code somewhere after your select boxes.
To set up the select options, first, add onChange="update(this)" as an attribute to the select element. Then for each select element, add all possible options under that element and set the class equal to the value of the prerequisite.
Here is a simple example:
<form name="formname" method="post" action=""> <select name="one" onChange="update(this)"> <option value="">Select #1</option> <option value="numbers">Select Numbers</option> <option value="letters">Select Letters</option> </select> <select name="two" onChange="update(this)"> <option value="">Select #2</option> <option value="1" class="numbers">1</option> <option value="2" class="numbers">2</option> <option value="3" class="numbers">3</option> <option value="a" class="letters">a</option> <option value="b" class="letters">b</option> <option value="c" class="letters">c</option> </select> <select name="three" onChange="update(this)"> <option value="">Select #3</option> <option value="1.1" class="1">1.1</option> <option value="1.2" class="1">1.2</option> <option value="2.1" class="2">2.1</option> <option value="2.2" class="2">2.2</option> <option value="3.1" class="3">3.1</option> <option value="3.2" class="3">3.2</option> <option value="a.1" class="a">a.1</option> <option value="a.2" class="a">a.2</option> <option value="b.1" class="b">b.1</option> <option value="b.2" class="b">b.2</option> <option value="c.1" class="c">c.1</option> <option value="c.2" class="c">c.2</option> </select> </form>In this example, we want the options a,b, and c, to show up when the user selects "letters" under the first drop down. Therefore, we set the class equal to "letters". Etc...
And here is the javascript to be inserted anywhere after the select options:
<script type="text/javascript">
var s=Array();
var c=Array();
var a=document.getElementsByTagName("select");
for(i=0;i<a.length;i++){
b=a[i].options;
s[i]=Array();
c[i]=Array();
for(j=0,k=0;j<b.length;k++){
s[i][k]=b[j];
c[i][k]=b[j].className;
if(c[i][k]!="")b[j]=null;
else j++;
}
}
function update(obj){
for(n=0;n<a.length && obj!=a[n];n++);
if(++n>=a.length)return;
while(a[n].options.length>1)a[n].options[1]=null;
for(i=0;i<s[n].length;i++)
if(c[n][i]=="" || c[n][i]==obj.value)
a[n].options[a[n].options.length]=s[n][i];
a[n].value="";
}
</script>
And now putting it all together using your structure of countries->airports->cities...
<form name="formname" method="post" action="">
<select name="Country" onChange="update(this)">
<option value="">Select Country</option>
<option value="Spain">Spain</option>
<option value="Turkey">Turkey</option>
<option value="Greece">Greece</option>
</select>
<select name="Airport" onChange="update(this)">
<option value="">Select Airport</option>
<option value="Barcelona" class="Spain">Barcelona</option>
<option value="Gerona" class="Spain">Gerona</option>
<option value="Malaga" class="Spain">Malaga</option>
<option value="Alicante" class="Spain">Alicante</option>
<option value="Madrid" class="Spain">Madrid</option>
<option value="Antalya" class="Turkey">Antalya</option>
<option value="Bodrum" class="Turkey">Bodrum</option>
<option value="Dalaman" class="Turkey">Dalaman</option>
<option value="Istambul" class="Turkey">Istambul</option>
<option value="Rhodes" class="Greece">Rhodes</option>
<option value="Athens" class="Greece">Athens</option>
<option value="Heraklion" class="Greece">Heraklion</option>
<option value="Corfu" class="Greece">Corfu</option>
<option value="Zante" class="Greece">Zante</option>
</select>
<select name="City" onChange="update(this)">
<option value="">Select City</option>
<option value="Barcelona was selected #1" class="Barcelona">Barcelona was selected #1</option>
<option value="Barcelona was selected #2" class="Barcelona">Barcelona was selected #2</option>
<option value="Rhodes was selected #1" class="Rhodes">Rhodes was selected #1</option>
<option value="Rhodes was selected #2" class="Rhodes">Rhodes was selected #2</option>
[Replace me with more options for each airport...]
</select>
</form>
<script type="text/javascript">
var s=Array();
var c=Array();
var a=document.getElementsByTagName("select");
for(i=0;i<a.length;i++){
b=a[i].options;
s[i]=Array();
c[i]=Array();
for(j=0,k=0;j<b.length;k++){
s[i][k]=b[j];
c[i][k]=b[j].className;
if(c[i][k]!="")b[j]=null;
else j++;
}
}
function update(obj){
for(n=0;n<a.length && obj!=a[n];n++);
if(++n>=a.length)return;
while(a[n].options.length>1)a[n].options[1]=null;
for(i=0;i<s[n].length;i++)
if(c[n][i]=="" || c[n][i]==obj.value)
a[n].options[a[n].options.length]=s[n][i];
a[n].value="";
}
</script>
Additional notes:
I wrote this javascript myself from scratch. Anyone can use it for any reason without giving any credit to me.
(I hate people who write simple code like this and add their name in the comments. I mean seriously, you didn't write a library, it's a simple piece of code that took 30 seconds to write... </rant>)
Post-post script:
I know my coding style isn't very readable, but this is ideally how people should write javascript. Javascript is interpreted therefore you should write it as minimal as possible to cut down on loading times. Consider yourself lucky that I didn't remove all whitespace and semicolons before close brackets.





Find content
Not Telling
Display name history
