populate one dropdownlist from another using jquery | dynamic select box | dynamic dropdown
Below is the fix for how to populate one dropdown with another using json and Jquery.
HTML
Jquery
$("#json-one").change(function() {
var $dropdown = $(this);
$.getJSON("data.json", function(data) {
var key = $dropdown.val();
var vals = [];
switch(key) {
case 'beverages':
vals = data.beverages.split(",");
break;
case 'snacks':
vals = data.snacks.split(",");
break;
case 'base':
vals = ['Please choose from above'];
}
var $jsontwo = $("#json-two");
$jsontwo.empty();
$.each(vals, function(index, value) {
$jsontwo.append("");
});
});
});
JSON
{
"beverages": "Coffee,Coke,beer",
"snacks": "Chips,Cookies"
}
2 Responses to populate one dropdownlist from another using jquery | dynamic select box | dynamic dropdown
Leave a Reply Cancel reply
Latest posts
- bootstrap popover | bootstrap popover hide on click | bootstrap popover hide on document.click
- Avoid console errors in browsers that lack a console | HTML5 boilerplate
- getting started with Sublime editor | how to install zen coding | zen coding for sublime editor
- getting started with backbone | Models in Backbone
- How to delete a model in backbone







Good web site! I truly love how it is easy on my eyes and the data are well written. I’m wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which must do the trick! Have a great day!
You should not append each option to the DOM like that. If anything, create a document fragment to append them to first and then append the document fragment. There are various other methods, such as building a string for all the options and setting the innerHTML or creating the collection via jQuery.