Refresh - Add Example
Try the backbone.js library? This will make Javascript more manageable by adding models and structures. However, there is a learning curve, but it is really great. After learning Backbone, you can use the Backbone Forms plugin to help you manage your drop-down list. Below is a demo link and sample code:
Example 1
$(function() { var cities = { 'UK': ['London', 'Manchester', 'Brighton', 'Bristol'], 'USA': ['London', 'Los Angeles', 'Austin', 'New York'] }; var subAreas = { 'London' : ['L1', 'L2', 'L3', 'L4'], 'Manchester' : ['M1', 'M2', 'M3', 'M4'], 'Brighton' : ['B1', 'B2', 'B3', 'B4'], 'Bristol' : ['BR1', 'BR2', 'BR3', 'BR4'], 'Los Angeles' : ['LA1', 'LA2', 'LA3', 'LA4'], 'Austin' : ['A1', 'A2', 'A3', 'A4'], 'New York' : ['NY1', 'NY2', 'NY3', 'NY4'] }; //The form var form = new Backbone.Form({ schema: { country: { type: 'Select', options: ['UK', 'USA'] }, city: { type: 'Select', options: cities.UK }, subArea: { type: 'Select', options: subAreas[cities.UK[0] ] } } }).render(); form.on('country:change', function(form, countryEditor) { var country = countryEditor.getValue(), newOptions = cities[country]; form.fields.city.editor.setOptions(newOptions); var city = newOptions[0], areaOptions = subAreas[city]; form.fields.subArea.editor.setOptions(areaOptions); }); form.on('city:change', function(form, cityEditor) { var city = cityEditor.getValue(), newOptions = subAreas[city]; form.fields.subArea.editor.setOptions(newOptions); }); //Add it to the page $('body').append(form.el);
});
Example 2
$(function() { var cities = { 'UK': ['London', 'Manchester', 'Brighton', 'Bristol'], 'USA': ['London', 'Los Angeles', 'Austin', 'New York'] }; var subAreas = { 'UK.London' : ['L1', 'L2'], 'USA.London' : ['L3', 'L4'], 'UK.Manchester' : ['M1', 'M2', 'M3', 'M4'], 'UK.Brighton' : ['B1', 'B2', 'B3', 'B4'], 'UK.Bristol' : ['BR1', 'BR2', 'BR3', 'BR4'], 'USA.Los Angeles' : ['LA1', 'LA2', 'LA3', 'LA4'], 'USA.Austin' : ['A1', 'A2', 'A3', 'A4'], 'USA.New York' : ['NY1', 'NY2', 'NY3', 'NY4'] }; var hashFunc = function(country, city){ return country + "." + city; }; //The form var form = new Backbone.Form({ schema: { country: { type: 'Select', options: ['UK', 'USA'] }, city: { type: 'Select', options: cities.UK }, subArea: { type: 'Select', options: subAreas[ 'UK.London' ] } } }).render(); form.on('country:change', function(form, countryEditor) { var country = countryEditor.getValue(), newOptions = cities[country]; form.fields.city.editor.setOptions(newOptions); var city = newOptions[0], areaOptions = subAreas[hashFunc(country, city) ]; form.fields.subArea.editor.setOptions(areaOptions); }); form.on('city:change', function(form, cityEditor) { var city = cityEditor.getValue(), newOptions = subAreas[hashFunc(form.getValue().country, city)]; form.fields.subArea.editor.setOptions(newOptions); }); //Add it to the page $('body').append(form.el); });
As you are also developing for mobile devices (possibly Phonegap), you can also try ZeptoJS as an alternative to jQuery. This will greatly improve speed.