Skip to content Skip to sidebar Skip to footer

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 o

Solution 1:

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: 5px5px05px;
  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;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><liclass="dropdown"><aclass="drop"href="#"><span>Dropdown</span><iclass="down-caret"></i></a></li>

Solution 2:

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: 5px5px05px;
  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;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><liclass="dropdown"><aclass="drop"href="#"><span>Dropdown</span><iclass="down-caret"></i></a></li>

Here is the fiddle

Solution 3:

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.

Solution 4:

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')

Solution 5:

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

Post a Comment for "Button's Event Doesn't Work After Click On Button's Title Or Button's Icon"