Demon King
Demon King

Reputation: 97

my dropdown button isnt clickable over the text but is clickable anywhere else on the button

I have a dropdown button called options I can click anywhere around the button and the dropdown opens but when I click along the text of the button in the middle it doesn't open, however I have used the same code on other dropdown buttons and they work just fine. this button only stopped working properly when I changed the function name in JS any help please? all the button code is below.

 <div class="options-select">
          <button onclick="options()" class="options-dropbtn"><span>Options</span> &#9660;</button>
          <div id="options-Dropdown" class="options-dropdown-content">
            <a href="#home">List similar item</a>
            <a href="#home">See similar items</a>
            <a href="#about">View trader's other items</a>
          </div>
        </div>

function options() {
  document.getElementById("options-Dropdown").classList.toggle("show");
}

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


.options-dropbtn {
  background-color: #fd886b;
  color: white;
  font-size: 16px;
  cursor: pointer;
  position: relative;
  display: block;
  width: 250px;
  height: 50px;
  border: 1px solid orangered;
  left: 75px;
  top: 50px;
}

.options-dropbtn:hover, .dropbtn:focus {
  background-color: #f76f4d;
}

.options-dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  width: 250px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
  text-align: center;
  top: 302px;
  left: 75px;
}

.options-dropdown-content a {
  color: #818181;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  z-index: 1;
}

.options-dropdown-content a:hover {
  color: white;
  transition: 0.5s;
}

.options-select a:hover {background-color: orangered;}

.show {
  display: block;
}

Upvotes: 1

Views: 34

Answers (1)

s.kuznetsov
s.kuznetsov

Reputation: 15223

This is due to the span tag inside the button tag, and therefore the click function ( onclick="options()" ) is set on the button. Add this rule to your css:

button.options-dropbtn span {
  pointer-events: none;
}

function options() {
  document.getElementById("options-Dropdown").classList.toggle("show");
}

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

.options-dropbtn {
  background-color: #fd886b;
  color: white;
  font-size: 16px;
  cursor: pointer;
  position: relative;
  display: block;
  width: 250px;
  height: 50px;
  border: 1px solid orangered;
  left: 75px;
  top: 50px;
}

.options-dropbtn:hover, .dropbtn:focus {
  background-color: #f76f4d;
}

.options-dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  width: 250px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
  text-align: center;
  top: 302px;
  left: 75px;
}

.options-dropdown-content a {
  color: #818181;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  z-index: 1;
}

.options-dropdown-content a:hover {
  color: white;
  transition: 0.5s;
}

.options-select a:hover {background-color: orangered;}

.show {
  display: block;
}
 <div class="options-select">
          <button onclick="options()" class="options-dropbtn"><span>Options</span> &#9660;</button>
          <div id="options-Dropdown" class="options-dropdown-content">
            <a href="#home">List similar item</a>
            <a href="#home">See similar items</a>
            <a href="#about">View trader's other items</a>
          </div>
        </div>

Upvotes: 1

Related Questions