jt691
jt691

Reputation: 75

Icon interfering with button click event

I have a button enclosing an icon. I believe the icon is interfering with my click event. I am only able to click on the icon margins to activate the onclick event, but nothing happens when I click on the icon. I replaced the icon with some text and the button onclick works perfectly fine. I have tried z-index to put the icon behind the button, but to no avail. Can someone explain why the icon blocks the click from occurring and how I can fix it?

html:

<div class="navMenu">
     <button onclick="navClick()" class="navMenu-button"><i class="fas fa-bars"></i></button>
     <div id="navList" class="navMenu-content">
           <a href="index.htm" class="navMenu-link">Home</a>
           <a href="about.htm" class="navMenu-link">About</a>
           <a href="resume.htm" class="navMenu-link">Resume</a>
     </div>

sass:

    .navMenu{
    position: relative;
    display: inline-block;
    margin-top:5px;
    margin-left:5px;
    &-button {
      background-color: #615b5b;
      border-radius:50%;
      color: white;
      padding: 7px;
      opacity:0.7;
      border:none;
      font-size: 14px;
      cursor: pointer;
    }
    &-button:hover, &-button:focus {
      background-color: #615b5b;
    }
    &-content {
      display: none;
      position: absolute;
      background-color: #f1f1f1;
      min-width: 200px;
      box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
      z-index: 1;
    }
    &-content .navMenu-link{
      color: $body-text-color;
      padding: 12px 16px;
      text-decoration: none;
      display: block;
    }
    &-content .navMenu-link:hover {
        background-color: #ddd;
    }
    &-show {
        display:block;
    }
}

js:

function navClick() {
document.getElementById("navList").classList.toggle("navMenu-show");
}
window.onclick = function(event) {
if (!event.target.matches('.navMenu-button')) {
     var dropdowns = document.getElementsByClassName("navMenu-content");
     var i;
     for (i = 0; i < dropdowns.length; i++) {
       var show = dropdowns[i];
       if (show.classList.contains('navMenu-show')) {
          show.classList.remove('navMenu-show'); 
        }
     }
   }
}

Upvotes: 5

Views: 2620

Answers (1)

Renzo Calla
Renzo Calla

Reputation: 7696

This is happening becuase your are setting an event that verifies if the element clicked contains an especific class, and indeed when it clicks the icon, it won't match because the icon does not contains the class you can solve it asking if also the parent contains the class....

window.onclick = function(event) {

   if (event.target.matches('.navMenu-button') || 
      event.target.parentNode.matches('.navMenu-button')
   ) {
    console.log("it matches..."); 
   }
}
.icon {
background:red;
}
<button class="navMenu-button">this is the button
  <div class="icon">this is the icon</div>
</button>

On the other hand you could reference the click event using the "onclick" method in this case it will solve it automatically..

var button = document.querySelectorAll('.navMenu-button')[0];
button.onclick = function() {
  console.log("button clicked");
}

Upvotes: 4

Related Questions