How to select certain elements in a group of elements with the same class tag using jquery

Viewed 57

I am trying this sidebar of a dashboard and it looks like this

enter image description here

The top child or the one with the brighter green color on it has a class active_link which basically tells you that you are in this part of the dashboard. So the idea is that I want the class active link to be added to the certain element that I have clicked upon while the rest who are not clicked don't have (Meaning that if the element already have the active_link but it is not the one that I have clicked upon then the class will be removed however if it does not have the active_link class and it is the one I clicked it will be added to that certain element). However, I have a problem.I tried running this code:

  $(function(){
  $(".sidebar_link").click(function(){
      $(".sidebar_link").each(function(index){
          if($(this).data('clicked', true)){
            if($(this).hasClass('active_link') == false){
              $(this).addClass('active_link');
            }
          }else{
            if($(this).hasClass('active_link')){
              $(this).removeClass('active_link');
            }
          }
      });
  });

})

And this is what happened:

enter image description here

Which what I could tell is just a disaster, any tips? Thank you in advance! Here is the HTML

<div class="sidebar_menu">
  <div class="sidebar_link active_link">
    <i class="fa fa-home"></i>
    <a href="#">Dashboard</a>
  </div>
  <h2>MANAGE</h2>

  <div class="sidebar_link">
    <i class="fa fa-building-o"></i>
    <a href="#">See Admin Notif</a>
  </div>
  <div class="sidebar_link">
    <i class="fa fa-wrench"></i>
    <a href="#">Update Profile</a>
  </div>
  <div class="sidebar_link">
    <i class="fa fa-archive"></i>
    <a href="#">Donate</a>
  </div>
  <div class="sidebar_link">
    <i class="fa fa-handshake-o"></i>
    <a href="#">Connect</a>
  </div>

  <div class="sidebar_logout">
    <i class="fa fa-power-off"></i>
    <a href="#">Log out</a>
  </div>
</div>
1 Answers

You can simply use $(".sidebar_link").not($(this)).removeClass("active_link") to remove class from other sidebar which is not been clicked by user.

Demo Code :

$(".sidebar_link").click(function() {
  $(".sidebar_link").not($(this)).removeClass("active_link")
  $(this).toggleClass('active_link');
});
.active_link {
  background-color: green
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="sidebar_menu">
  <div class="sidebar_link active_link">
    <i class="fa fa-home"></i>
    <a href="#">Dashboard</a>
  </div>
  <h2>MANAGE</h2>

  <div class="sidebar_link">
    <i class="fa fa-building-o"></i>
    <a href="#">See Admin Notif</a>
  </div>
  <div class="sidebar_link">
    <i class="fa fa-wrench"></i>
    <a href="#">Update Profile</a>
  </div>
  <div class="sidebar_link">
    <i class="fa fa-archive"></i>
    <a href="#">Donate</a>
  </div>
  <div class="sidebar_link">
    <i class="fa fa-handshake-o"></i>
    <a href="#">Connect</a>
  </div>

  <div class="sidebar_logout">
    <i class="fa fa-power-off"></i>
    <a href="#">Log out</a>
  </div>
</div>

Related