Prevent Dropdown Menu from closing with Right Click inside it

Asked

Viewed 79 times

0

I would like this Dropdown menu not to close when right-clicking inside it to open a link in a new tab.

/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
} 
 /* Dropdown Button */
.dropbtn {
    background-color: #3498DB;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
    background-color: #2980B9;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
    position: relative;
    display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

/* Links inside the dropdown */
.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;} 
 <div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div> 

  • Remove the code from window.onclick

  • Left or right click?

  • @dvd is right, I got confused here, which opens the options of Browser, "Open in a new tab" etc...

  • Please change the question or the question becomes meaningless.

1 answer

1


From what I’ve seen, it only happens in Firefox Quantum 57, that captures a right mouse click on the event onclick, although it does not act on the element (as if it were a left click).

To solve this, you can add another Event Handler in the submenu with oncontextmenu, that detects the right mouse click. In this case you can add the class .show to the element. This will cause it not to close even by right-clicking.

As stated at the beginning, it should be tested in FF Quantum 57. In other browsers there is no such problem:

/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
      document.querySelector(".dropdown-content").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}   

var drop = document.querySelector(".dropdown-content");
drop.oncontextmenu = function(event) {
   drop.classList.add("show");
}
/* Dropdown Button */
.dropbtn {
    background-color: #3498DB;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
    background-color: #2980B9;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
    position: relative;
    display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

/* Links inside the dropdown */
.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}
<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

  • Perfect. That’s exactly it. Thank you.

  • I intend to use 2 or even 3 of these dropdowns on the same page, I tried here, but there are conflicts. How should I proceed? I wanted something similar to these dropdown menus with Stackoverflow icons. Of course I tried to steal them, but to no avail.. rsrs

  • @Johnquimera See that the menu has an id="myDropdown"... if you duplicate it will give conflict because the script does not know how to differentiate an id from the other, it will always get the first one it finds... it would have to adapt the code and exchanging id for class.. as for the icons, has a plugin that already comes with several icons, I think it is the font-awesome

  • i already switched all Document.getElementById() to Document.querySelector() and now I’m only using classes, it’s working the same way, but it doesn’t work two on the same page.

  • @Johnquimera You have to do querySelectorAll to select all and then make a loop

  • has a dropdown that is equal to this, in jquery, supports several of these, I just need to do the same left click scheme and solve the problem of closing the other dropdown when another one opens. https://codepen.io/johnquimera/pen/RxzEQr

  • @Johnquimera Asks questions that we try to solve.

  • https://answall.com/questions/271977/dropdowns-multiplos-na-mesma-p%c3%a1gina

  • Give me a hand. https://answall.com/questions/276470/jquery-dropdown-parar-propaga%C3%A7%C3%A3o-com-click-right? noredirect=1

  • Blz, I see here.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.