A dependent dropdown changes the options in a second <select> according to the value chosen in the first. The original example uses three categories—Fruits, Games and Scripts—and fills the second list entirely in the browser.
Instead of scattering many if statements through the page, keep the relationships in one object.
const subcategories = {
Fruits: ["Mango", "Banana", "Orange"],
Games: ["Cricket", "Football", "Polo"],
Scripts: ["PHP", "ASP", "JavaScript"]
};
function fillSubcategories(category, target) {
target.replaceChildren(new Option("Subcategory", ""));
for (const item of subcategories[category] ?? []) {
target.add(new Option(item, item));
}
}
replaceChildren() clears the old options, and new Option() creates each replacement option. This is the same purpose as the original adding-options tutorial, but with less DOM bookkeeping.
category.addEventListener("change", () => {
fillSubcategories(category.value, subcategory);
});
The old version populated the first list during body onload and called an inline onChange handler. The modern demo loads its JavaScript with defer and registers listeners in the script.
After creating the options, set target.value to a valid option value when a default is required.
fillSubcategories("Fruits", subcategory);
subcategory.value = "Mango";
The original page taught reusable removeAllOptions() and addOption() helpers. They remain useful for understanding the DOM operations, but modern APIs make the same job shorter:
target.replaceChildren();
target.add(new Option("Mango", "Mango"));
For server-generated data, see the related PHP dynamic dropdown tutorial.
Download the updated static dependent-dropdown example. The archive now matches the modern event-listener version shown here.
Continue with dependent dropdowns from MySQL table data.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Webbieads | 03-08-2010 |
| Great, script, I want to use this but have two forms (exactly the same) on one html page and cannot implement this script for both - do you know how I could do that? | |
| Tom | 11-02-2011 |
| The code does not work if allowing user to make multiple selections (i.e. multiple="multiple"). Please add update for this in your tutorial. Thank you! | |
| sardar | 19-03-2011 |
| how to integrate ajax with multiple selection with dropdown list.is there any solution for that your help will be appreciated | |
| newbie | 31-05-2011 |
| Can you tell me how to encode categorie and subcategorie values to display in utf-8 format? | |
| James | 01-06-2011 |
| I need to have multiple instances of the Double drop down list box on my form (I need to use it three time). I have been unable to figure this out, can anyone offer assistance? | |
| andy | 05-07-2011 |
| My programming experience is minimal, but I was able to follow this tutorial relatively easily and implement it (dynamic population of second list box...) within a couple of hours, and now I´m very happy - thank you! | |
| vinu | 15-03-2012 |
| hai this nice then display the coding all of u used it | |
| debbieh | 17-05-2014 |
| I want the results of two dropdown box selections 1) State 2) City to then populate a page of business listings (specific categories)-- let's say they are city, county & federal courthouses... (from there I will link to 'city=Atlanta "Atlanta city courthouse" aref="http*URL"--- for example)... can you help me? How do I get the SUBMIT search button to then populate the page with results? (i.e. courthouses) | |
| mehul | 05-07-2014 |
| can you give example of 3 dynamic dropdown menus... m realy stuck in coding and which i found is not working in case of space in any category string.. please help me | |
| smo | 05-07-2014 |
| You can keep category name within single quotes. That should work. | |
| stephen | 22-09-2014 |
| I need to be able to use one drop down list to populate three or more fields | |
| Ajay | 13-12-2014 |
| nice script | |