How to avoid dropdown menu to close menu items on clicking inside ?
The default behavior of a dropdown menu is to close the menu list items when clicked inside. In this article, We will use stropPropagation method to prevent the dropdown menu from closing the menu list.
stopPropagation(): The stopPropagation() method is used to stop propagation of event calling i.e. the parent event is called we can stop the propagation of calling its children by using the stopProagration() method and vice-versa.
Syntax:
event.stopPropagation();
Example:
<!DOCTYPE html> <html> <head> <title> How to avoid dropdown menu to close menu items on click inside ? </title> <style> .dropbutton { background-color: #4CAF50; color: white; padding: 14px; font-size: 14px; border: none; cursor: pointer; } .dropbutton:hover, .dropbutton:focus { background-color: #3e8e41; } .dropdownmenu { position: relative; display: inline-block; outline: none; } .dropdownmenu-content { display: none; position: absolute; background-color: #f9f9f9; min-width: 140px; overflow: auto; box-shadow: 0px 8px 16px rgba(0,0,0,0.3); } .dropdownmenu-content a { color: black; padding: 12px 14px; text-decoration: none; display: block; } .dropdownmenu a:hover { background-color: #f1f1f1 } .show { display:block; } </style> </head> <body style="text-align:center;"> <h1 style="color:green">GeeksforGeeks</h1> <p> Clicking outside will close the drop down menu. </p> <div class="dropdownmenu"> <button onclick="btnToggle()" class="dropbutton"> Dropdown </button> <div id="Dropdown" class="dropdownmenu-content" > <a href="#Java">Java</a> <a href="#HTML">HTML</a> <a href="#CSS">CSS</a> <a href="#JS">JavaScript</a> </div> </div> <script> // JavaScript code to avoid dropdown // menu close // Clicking dropdown button will toggle display function btnToggle() { document.getElementById("Dropdown").classList.toggle("show"); } // Prevents menu from closing when clicked inside document.getElementById("Dropdown").addEventListener('click', function (event) { alert("click outside"); event.stopPropagation(); }); // Closes the menu in the event of outside click window.onclick = function(event) { if (!event.target.matches('.dropbutton')) { var dropdowns = document.getElementsByClassName("dropdownmenu-content"); var i; for (i = 0; i < dropdowns.length; i++) { var openDropdown = dropdowns[i]; if (openDropdown.classList.contains('show')) { openDropdown.classList.remove('show'); } } } } </script> </body> </html> |
Output:
- Click Inside:

- Click Outside:

Recommended Posts:
- How to open dropdown menu on hover in Bootstrap ?
- How to display div elements using Dropdown Menu in jQuery?
- HTML | DOM Menu Object
- How to create Radial Menu in CSS ?
- HTML Course | Creating Navigation Menu
- How to cover the whole screen when the hamburger menu is pressed on smaller screens?
- Open a link without clicking on it using JavaScript
- Sound generation on clicking the button using JavaScript
- How to change the background color after clicking the button in JavaScript ?
- How to display confirmation dialog when clicking an <a> link using JavaScript / jQuery ?
- How to style a dropdown using CSS?
- How to get selected value in dropdown list using JavaScript ?
- How to remove arrow in dropdown in Bootstrap ?
- How to avoid Compile Error while defining Variables
- How to avoid NullPointerException in Java using Optional class?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



