Here I am trying to create html drop down list. I have two things to select on my main list mobile
and laptop
. If I select the mobile option in my first list, the second drop down list has to load the options from mobile brand. and if I select laptop, it has to load laptop brand.Now the third list has to populate depends on the second list selection. suppose if I select samsung as mobile brand, it has to load the android version on next drop down menu.
My code(I have added the second menu list as separate here with same id.)
<select id="main_list">
<option value="default" selected>Select Your List</option>
<option value="mobile">mobile list</option>
<option value="laptop">laptop list</option>
</select>
<select id="brands">
<option value="default" selected>Select Your Mobile Brand</option>
<option value="mobile_1">Samsung</option>
<option value="mobile_2">Nokia</option>
</select>
<select id="brands">
<option value="default" selected>Select Your Laptop Brand</option>
<option value="laptop_1">HP</option>
<option value="laptop_2">Dell</option>
</select>
<select id="samsung_select">
<option value="default" selected>Select Your Andriod Version</option>
<option value="andriod_1">4.1</option>
<option value="andriod_2">4.2</option>
</select>
<select id="nokia_select">
<option value="default" selected>Select Your Windows Version</option>
<option value="windows_1">windows 8</option>
<option value="windows_2">windows 8.1</option>
</select>
what is the best way to populate this list automatically. Not using any db here.
I have seen a previous similar question here, but this is not working here for me
You're right. There have been plenty of similar questions, but there always seem to be little differences between them.
I've written widgets like this before. Here's an approach that's fairly different from gibberish's answer.
It involves a widget that you would use like this:
The handlers supplied to
onComplete
would receive objects such asYou would not use any markup at all. Instead it would be configured with an object like this:
Note that this could be a little different if you weren't using repeated names. The
id
s could be removed, andnextId
could becomenextName
.This is a naive implementation of such a widget:
You can see it in action on JSFiddle.
There are plenty of things that could be made better. The biggest issue I see is that the DOM location for the constructed SELECTS is extremely simplistic.
In any case, it's a different approach.
There's no getting around that it's a bit of tedious coding, but this should give you a good start:
jsFiddle Demo
HTML:
css:
js/jQuery: