Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to avoid dropdown menu to close menu items on clicking inside ?

  • Last Updated : 09 Jul, 2019

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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!




<!DOCTYPE html>
        How to avoid dropdown menu to close
        menu items on click inside ?
        .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 {
<body style="text-align:center;">
    <h1 style="color:green">GeeksforGeeks</h1> 
        Clicking outside will close
        the drop down menu.
    <div class="dropdownmenu">
        <button onclick="btnToggle()" class="dropbutton">
        <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>
        // JavaScript code to avoid dropdown
        // menu close
        // Clicking dropdown button will toggle display
        function btnToggle() {
        // Prevents menu from closing when clicked inside
        document.getElementById("Dropdown").addEventListener('click', function (event) {
            alert("click outside");
        // Closes the menu in the event of outside click
        window.onclick = function(event) {
            if (!'.dropbutton')) {
                var dropdowns = 
                var i;
                for (i = 0; i < dropdowns.length; i++) {
                    var openDropdown = dropdowns[i];
                    if (openDropdown.classList.contains('show')) {


  • Click Inside:
  • Click Outside:
    O/P after click outside

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!