Creating a Chained Select
This article is based on the question asked on Web Developer Forum on creating a category tree using select. Also known as chained select.
First, a description of chained select. It is a series of selection using select box where each sub-select depends on the previous selection.
Examples of this kind of chained select is usually encountered when downloading drivers, such as that found in NVIDIA website.
On NVIDIA website, the select boxes are all empty except the first one. While here I am going to create new select box if there is still a sub-selection. It can go on unlimited select box.
Onto the logic and codes:
First, I need to be able to dynamically create the select. For this, I used the code found on StackOverflow. I used the vanilla version. You might go with JQuery version, if you use that.
Once it is done, I simply modify the code to include onClick event on the select element.
The function included in onClick will check if there is still data to create the next select box. If there is, then it will create a new select box.
Very very simple.
Here is the full code:
Note that I created my own selection. Where in reality you would grab them using AJAX call to your database.
Hope you find it useful. Please share the article if you find it useful.