I have a select box like this:
<select name="cars" id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
I want to make the second option in the list a dropdown and display a few more select options.
Can anyone please tell me how to accomplish this?
Thanks in advance!
2
Answers
HTML Select can only display one level.
I shared an example, where we have two elements. The first one allows the user to choose a continent, while the second one allows the user to choose a country within the selected continent. The element groups the options within each continent.
Note that the label attribute of the element is used to display the name of the group. When the user selects a continent from the first element, the options in the second element are dynamically updated to show only the countries within that continent.
You can add a second
select
option and controlling when show or hide it.Example using pure javascript:
[Edited] Using two
select
with same name and values is possible too:You can use same name and values, only the id need to be unique.