JavaScript - How to Create Dropdown List? Last Updated : 26 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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 Dropdown List using JavaScriptWe will use JavaScript to toggle the list visibility dynamically. If the display property is none then change it to block and vice-versa.Syntaxconst element = document.getElementById("dropdown_list");if (element.style.display == "block") { element.style.display = "none";else { courses.style.display = "block";}Example: Creating a basic dropdown menu containing few items using JavaScript that toggles on click. HTML <!DOCTYPE html> <html> <head> <title>Dropdown List Using JavaScript</title> <style> .dropdown { width: 160px; padding: 10px; } .dropdown-button { padding: 10px; border: 1px solid #ccc; cursor: pointer; background-color: #f9f9f9; } /* Hide the Menu Items*/ .menu { display: none; position: absolute; background-color: #f9f9f9; border: 1px solid #ccc; min-width: 160px; } .menu-item { padding: 10px; cursor: pointer; } </style> </head> <body> <div class="dropdown"> <div class="dropdown-button" id="dropdownButton">Menu</div> <!-- Dropdown list items--> <div class="menu" id="optionsMenu"> <div class="menu-item">Item 1</div> <div class="menu-item">Item 2</div> <div class="menu-item">Item 3</div> </div> </div> <script> // JavaScript for dropdown functionality const dropdownButton = document.getElementById("dropdownButton"); const optionsMenu = document.getElementById("optionsMenu"); const dropdownItems = document.querySelectorAll(".menu-item"); // Toggle dropdown menu visibility dropdownButton.addEventListener("click", () => { if (optionsMenu.style.display === "block") { optionsMenu.style.display = "none"; } else { optionsMenu.style.display = "block"; } }); // Update the dropdown text and close the menu on item click dropdownItems.forEach((item) => { item.addEventListener("click", () => { optionsMenu.style.display = "none"; }); }); // Close dropdown menu if clicked outside document.addEventListener("click", (event) => { if (!event.target.closest(".dropdown")) { optionsMenu.style.display = "none"; } }); </script> </body> </html> Output Example 2: Creating a dropdown list that toggles the list visibility using JavaScript. HTML <!DOCTYPE html> <html lang="en"> <head> <title>Dropdown list using javascript</title> <style> .dropdown_button { background-color: #0979ad; color: white; padding: 16px; font-size: 16px; border: none; cursor: pointer; width: 200px; font-family: montserrat; border: 1px solid #ffffff; } .courses { display: none; /* Initially Hide the Dropdown List*/ position: absolute; background-color: #f1f1f1; min-width: 200px; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 1; } .courses li { color: black; padding: 12px 16px; text-decoration: none; display: block; list-style: none; background-color: rgb(47, 47, 47); font-family: montserrat; border: 1px solid white; } .courses li a { text-decoration: none; color: white; display: block; padding: 10px; } </style> </head> <body> <div class="main"> <div class="dropdown_list"> <button class="dropdown_button" onclick="toggleList()"> Select course </button> <div id="courses_container" class="courses"> <li><a href="">MERN Stack</a></li> <li><a href="">ReactJS Developer</a></li> <li><a href="">Backened Developer</a></li> <li><a href="">Machine Learning</a></li> <li><a href="">Data warehousing</a></li> </div> </div> </div> <script> function toggleList() { // Access the List const courses = document.getElementById("courses_container"); // Check the visibility if (courses.style.display == "block") { courses.style.display = "none"; // Hide the list } else { courses.style.display = "block"; // Display the list } } // Hide the list on click outside the dropdown list window.onclick = function (event) { if (!event.target.matches('.dropdown_button')) { document.getElementById('courses_container') .style.display = "none"; } } </script> </body> </html> Output We can also create the complete dropdowm list dynamically using JavaScript - Check this article. Comment More infoAdvertise with us Next Article JavaScript - How to Create Dropdown List? iamgaurav Follow Improve Article Tags : JavaScript Web Technologies CSS-Properties JavaScript-Methods HTML-Questions CSS-Questions JavaScript-Questions +3 More Similar Reads How To Create A Cascading Dropdown List Using JavaScript? 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 3 min read 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 How to Create Dropdown List in Google Sheets Adding a dropdown list in Google Sheets can make your spreadsheets more dynamic and user-friendly. This feature allows you to create a list of options that users can select from, making data entry quicker and reducing errors. Whether you're managing a budget, tracking projects, or organizing data, d 9 min read How to create a dropdown menu in MATLAB In this article, we will learn about the dropDown menu component, how to create it and what are its important properties. Then we will create a simple Matlab Gui application. Creating a dropdown menu in MATLABA Dropdown menu is a UI component that helps users to select one of the given options or ty 4 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 Dependent Drop Down List in Excel Dependent Down Lists allow you to validate the data and make the data entry and data filtering very smooth and time-saving. Dependent Down Lists increase the chances of fewer errors in one's worksheet. The prerequisites for learning Dependent Drop Down List are Create from Selection and Drop Down Li 9 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 How to style a dropdown using CSS? We will learn how to style the dropdown list using CSS and explore its implementation through examples. Dropdown menus are commonly used to allow users to select an option from a predefined list. Styling them properly can enhance your website's user experience and visual appeal.What is a <select 3 min read How to Create a Drop-Down List in Excel: 2 Easy Methods How to Build Drop Down Menu in Excel: Quick StepsOpen MS Excel Spreadsheet >> Enter List Items Select Cell >> Go to Data Validation Choose List Option Set the Source >>Click OKHave you ever typed the same data repeatedly in Excel, only to make typos or inconsistencies? A drop-down 9 min read How to define a drop-down list in HTML5 ? In this article, we define a Drop-Down List By using the <select> Element in the Document. This tag is used to create a drop-down list. The <select> tag contains <option> tag to display the available option of drop-down list.Note: The tag is used in a form to receive user response. 1 min read Like