How To Create A Cascading Dropdown List Using JavaScript? Last Updated : 09 Sep, 2024 Comments Improve Suggest changes Like Article Like Report The Cascading dropdown lists are used to display options in a secondary dropdown based on the selection made in a primary dropdown. This is useful in forms where users need to select categories or locations in the hierarchical manner. For example: selecting a country might reveal states or provinces specific to that country. PrerequisitesHTMLCSSJavaScriptThese are the approaches to create a cascading dropdown list using JavaScript:Table of ContentUsing JavaScript with HTML Select ElementsUsing JavaScript with Libraries (e.g. jQuery)Using JavaScript with HTML Select ElementsThis is the most basic method which involves using JavaScript to dynamically populate the options of the second dropdown based on the selection in the first dropdown.Example: Below example demonstrates dropdowns using HTML select elements for Country and a Dropdown for Select a state/province. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Cascading Dropdown Example </title> <style> body { font-family: Arial, sans-serif; } select { margin: 10px 0; padding: 5px; } </style> </head> <body> <label for="country"> Country:</label> <select id="country"> <option value=""> Select a country </option> <option value="usa"> USA</option> <option value="canada"> Canada</option> </select> <label for="state"> State/Province:</label> <select id="state"> <option value=""> Select a state/province </option> </select> <script> const states = { usa: ['California', 'Florida', 'New York'], canada: ['Ontario', 'Quebec', 'British Columbia'] }; document.getElementById('country'). addEventListener('change', function () { const country = this.value; const stateDropdown = document.getElementById('state'); stateDropdown.innerHTML = '<option value="">Select a state / province</option > '; if (states[country]) { states[country].forEach(state => { const option = document.createElement('option'); option.value = state.toLowerCase().replace(/\s+/g, '-'); option.textContent = state; stateDropdown.appendChild(option); }); } }); </script> </body> </html> Output:Using jQueryThis approach uses libraries like jQuery to the simplify the process of creating cascading dropdowns.The jQuery provides convenient methods for the DOM manipulation and event handling.Example: Below is the example demonstrating cascading dropdown using JavaScript with libraries having two dropdowns one for Country and one for Select a state/Province. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Cascading Dropdown with jQuery</title> <script src=" https://code.jquery.com/jquery-3.6.0.min.js"> </script> <style> body { font-family: Arial, sans-serif; } select { margin: 10px 0; padding: 5px; } </style> </head> <body> <label for="country">Country:</label> <select id="country"> <option value="">Select a country</option> <option value="usa">USA</option> <option value="canada">Canada</option> </select> <label for="state">State/Province:</label> <select id="state"> <option value="">Select a state/province</option> </select> <script> const states = { usa: ['California', 'Florida', 'New York'], canada: ['Ontario', 'Quebec', 'British Columbia'] }; $('#country').on('change', function () { const country = $(this).val(); const $stateDropdown = $('#state'); $stateDropdown.empty().append ('<option value=""> Select a state / province</option > '); if (states[country]) { states[country].forEach(state => { $stateDropdown.append(new Option(state, state.toLowerCase(). replace(/\s+/g, '-'))); }); } }); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How To Create A Cascading Dropdown List Using JavaScript? G gururaj69bn Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to Create a Dropdown List with Array Values using JavaScript? To add elements to the select element from the JavaScript array, We can also get selected values in the dropdown list using JavaScript. Here we will populate the dropdown list with an array.Below are the methods to create a dropdown list with array values:Table of ContentUsing Pure JavaScriptUsing j 2 min read JavaScript - How to Create Dropdown List? The dropdown list is a toggleable menu that allows to user to choose one option from multiple. The dropdown list is basically a button, when the user clicks on that, it expands downward and shows its sub-elements, from which users can select any one of them according to their preferences.Creating Dr 3 min read How to Create a Dropdown Menu with CSS & JavaScript ? A dropdown menu is a common user interface element that allows users to select from a list of options. It is often used in navigation bars or forms to conserve space and provide a clean user experience. Creating a dropdown menu involves a combination of CSS for styling and JavaScript for interactivi 3 min read How to Get Selected Value in Dropdown List using JavaScript? A dropdown list is an HTML element that allows users to select one option from a list of options. The <select> and <option> tags are used to create dropdown lists in HTML. In this article, we will see how to get selected value from dropdown list using JavaScript.Basic HTML Structure for 2 min read How to create a dropdown menu in Bootstrap ? A dropdown menu offers a list of alternatives when clicked or hovered on, which is a clean method of providing a list of alternatives as only one option is displayed initially onto the screen. Drop-down menus are used in almost all types of software nowadays to show sub-options of the option. Step 1 2 min read Like