Here is another example, simpler than the link (that I also +1d) in the comments
http://jsfiddle.net/joevallender/Y6Rjz/1/
Code below
HTML
<label for="continent">Continent</label>
<select id="continent">
<option value=""> -- Please select -- </option>
<option value="asia">Asia</option>
<option value="europe">Europe</option>
</select>
<label for="country">Country</label>
<select id="country"></select>
JS
var data = {
asia: [
'china',
'japan'
],
europe: [
'france',
'germany'
]
}
$('#continent').change(function(){
var value = $(this).val();
var list = data[value];
$('#country').empty();
$('#country').append(new Option(' -- Please select -- ',''));
for(var i = 0; i < list.length; i++) {
$('#country').append(new Option(capitaliseFirstLetter(list[i]),list[i]))
}
})
// Helper from http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript
function capitaliseFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}