Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 982
bahman parsamanesh Avatar asked Jan 20 '26 14:01

bahman parsamanesh


2 Answers

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>
like image 183
Jordan Quartermain Avatar answered Jan 23 '26 04:01

Jordan Quartermain


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

like image 44
Muhammad Usman Avatar answered Jan 23 '26 02:01

Muhammad Usman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!