Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Luis Magaña
13,042 PointsDependent DropDown (Dealership Model)
I need some help expanding on this code that I found on css-tricks.com. This is a dependent dropdown that works for two select elements I want to expand it to work for three, the third being dependent on the second. I am not the best with Jquery and my original attempt was two recreate the same function just change the Id's inside the script to match new select elements. Any help with this is much appreciated.
<select id="first-choice">
<option selected value="base">Please Select</option>
<option value="outdoorpatio">Outdoor Patio</option>
<option value="grills">Grills</option>
</select>
<br>
<select id="second-choice">
<option>Please choose from above</option>
</select>
<br>
<select id="third-choice">
<option>Please choose from above</option>
</select>
$("#first-choice").change(function() {
var $dropdown = $(this);
$.getJSON("jsondata/data.json", function(data) {
var key = $dropdown.val();
var vals = [];
switch(key) {
case 'outdoorpatio':
vals = data.outdoorpatio.split(",");
break;
case 'grills':
vals = data.grills.split(",");
break;
case 'base':
vals = ['Please choose from above'];
}
var $secondChoice = $("#second-choice");
$secondChoice.empty();
$.each(vals, function(index, value) {
$secondChoice.append("<option>" + value + "</option>");
});
});
});