Button's event doesn't work after click on button's title or button's icon

I created a dropdown button with tag and a "down-caret" icon. when dropdown menu is open, "down-caret" icon should rotate up and this is working.

But if I click on button title or "down-caret" icon, this event not work.

$(document).ready(function() {
  $('.dropdown').click(function(e) {
    $($(e.target).find('.down-caret').toggleClass('open-caret'));
    e.stopPropagation();
    $(document).click(function() {
      $('.dropdown-menu').removeClass('open');
      $('.down-caret').removeClass('open-caret');
    });
  });
});
a {
  text-decoration: none;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown .drop {
  background-color: #3498db;
  color: #fff;
  padding: 1rem;
  border-radius: 6px;
  position: relative;
  display: inline-block;
}

.down-caret {
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 5px 5px 0 5px;
  border-color: #ffffff transparent transparent transparent;
  display: inline-block;
  margin-left: 6px;
  top: -3px;
  position: relative;
  transform: rotate(0deg);
  transition: all 0.25s ease-in;
}

.open-caret {
  transform: rotate(180deg);
  transition: all 0.25s ease-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="dropdown">
  <a class="drop" href="#">
    <span>Dropdown</span>
    <i class="down-caret"></i>
  </a>
</li>

I need span & i tags to be exist in my code.

here is my complete code jsFiddle

5 answers

  • answered 2018-07-11 06:04 Saurabh Sharma

    I checked your fiddle code, and i just removed <span> and its working fine.
    I also updated your fiddle JSFiddle

  • answered 2018-07-11 06:04 Jordan Quartermain

    Just need to target the anchor element and change your JS slightly.

    $(document).ready(function(){
      $('a.drop').click(function(e){
        e.preventDefault();
        $('.down-caret',this).toggleClass('open-caret');
        e.stopPropagation();
        $(document).click(function(){
          $('.dropdown-menu').removeClass('open');
          $('.down-caret').removeClass('open-caret');
        });
      });
    });
    a {
      text-decoration: none;
    }
    .dropdown {
      position: relative;
      display: inline-block;
    }
    .dropdown .drop {
      background-color: #3498db;
      color: #fff;
      padding: 1rem;
      border-radius: 6px;
      position: relative;
      display: inline-block;
    }
    .down-caret {
      width: 0;
      height: 0;
      border-style: solid;
      border-width: 5px 5px 0 5px;
      border-color: #ffffff transparent transparent transparent;
      display: inline-block;
      margin-left: 6px;
      top: -3px;
      position: relative;
      transform: rotate(0deg);
      transition: all 0.25s ease-in;
    }
    .open-caret {
      transform: rotate(180deg);
      transition: all 0.25s ease-out;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <li class="dropdown">
       <a class="drop" href="#">
          <span>Dropdown</span>
          <i class="down-caret"></i>
       </a>
    </li>

  • answered 2018-07-11 06:09 George Bailey

    The reason is, you are using currently clicked element $(e.target) to find an element inside with class down-caret.

    And if you click on icon or the text there is no element inside with that specified class.

    You just need to change $(e.target) to $('.dropdown') and you are done.

    $(document).ready(function(){
      $('.dropdown').click(function(e){
        $('.dropdown').find('.down-caret').toggleClass('open-caret');
     
      });
      
     
    });
    a {
      text-decoration: none;
    }
    .dropdown {
      position: relative;
      display: inline-block;
    }
    .dropdown .drop {
      background-color: #3498db;
      color: #fff;
      padding: 1rem;
      border-radius: 6px;
      position: relative;
      display: inline-block;
    }
    .down-caret {
      width: 0;
      height: 0;
      border-style: solid;
      border-width: 5px 5px 0 5px;
      border-color: #ffffff transparent transparent transparent;
      display: inline-block;
      margin-left: 6px;
      top: -3px;
      position: relative;
      transform: rotate(0deg);
      transition: all 0.25s ease-in;
    }
    .open-caret {
      transform: rotate(180deg);
      transition: all 0.25s ease-out;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    
    <li class="dropdown">
       <a class="drop" href="#">
          <span>Dropdown</span>
          <i class="down-caret"></i>
       </a>
    </li>

    Here is the fiddle

  • answered 2018-07-11 06:29 Code_Ninja

    All you have to do is add this class in your CSS and the code will work as you want it to:

    .drop{
      position:relative;
      z-index: -1;
    }
    

    Here is the fiddle supporting it.

    Hope this was helpful.

  • answered 2018-07-11 06:33 david

    Instead of using target to find the "down-caret" icon, why don't just take straight "down-icon"?

    Change $(e.target).find('.down-caret') to $('.down-caret')