-1

When i click the tabs the color of the li has to be changed. I tried to remove the previous active class and add active class to the current li. but not working.

<script>
  $(document).ready(function () {
    $('.Tabs').tabs();
});

$('#tabs ul li a').click(function(){
    $(this).prev().find('li.Active').removeClass('Active');
  $(this).find('li').addClass('Active');
});
</script>

I have attached the jsfiddle https://jsfiddle.net/0frr7qn2/6/

VXp
  • 11,598
  • 6
  • 31
  • 46
Sindhu
  • 100
  • 3
  • 14

3 Answers3

3

I've made some corrections to your jQuery to give you the desired result:

$('#tabs ul li').click(function(){
  $(this).addClass('Active').siblings().removeClass('Active');
});
#tabs {
  border-top: 1px solid silver;
  border-bottom: 1px solid silver;
  border-right: white;
  background-color: #002750;
  color: white;
}

#tabs ul {
  padding: 6px 0 24px;
  margin: 0;
}

#tabs ul li {
  list-style-type: none;
  float: left;
  margin: 0 30px;
  cursor: pointer;
}

#tabs ul li.Active a {
  color: #b32218;
}

#tabs {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="Tabs">
  <div id="tabs">
    <ul>
      <li class="Active">
        <a href="#tab1">Information</a>
      </li>
      <li>
        <a href="#tab2">Accounting</a>
      </li>
      <li>
        <a href="#tab3">Tax Performance</a>
      </li>
    </ul>
  </div>
  <div id="tab1" class="Tab" style="display:none">
    <p>Tab 1 Content</p>
  </div>
  <div id="tab2" class="Tab" style="display:none">
    <p>Tab 2 Content</p>
  </div>
  <div id="tab3" class="Tab" style="display:none">
    <p>Tab 3 Content</p>
  </div>
</div>

I've optimized the code to be as short as possible. Since you have the .Active class on the li, I've changed the target to #tabs ul li so that you can take advantage of the .siblings() method.

VXp
  • 11,598
  • 6
  • 31
  • 46
1

I forked your jsfiddle here: https://jsfiddle.net/g5t3xhj1/

It works, here's what I did:

  • Updated your css to have active class on the a href instead of the li
  • Updated your jQuery to remove all active classes on click and add it back to the one clicked only

HTML

    <div class="Tabs">
  <div id="tabs">
    <ul>
      <li>
        <a class="active" href="#tab1">Information</a>
      </li>
      <li>
        <a href="#tab2">Accounting</a>
      </li>
      <li>
        <a href="#tab3">Tax Performance</a>
      </li>
    </ul>
  </div>
  <div id="tab1" class="tab" style="display:none">
    <p>Tab 1 Content</p>
  </div>

  <div id="tab2" class="tab" style="display:none">
    <p>Tab 2 Content</p>
  </div>

  <div id="tab3" class="tab" style="display:none">
    <p>Tab 3 Content</p>
  </div>
</div>

JS

 $(document).ready(function() {
   //Hide all Tabs on laod
   $('.Tabs').tabs();

 });

 $('#tabs ul li a').click(function() {
   $('#tabs ul li a').removeClass('active');
   $(this).addClass('active');
 });

CSS

li a.active {
  color: red;
}

#tabs {
  border-top: 1px solid silver;
  border-bottom: 1px solid silver;
  border-right: white;
  background-color: #002750;
  color: white;
}

#tabs ul {
  padding: 6px 0 24px;
  margin: 0;
}

#tabs ul li {
  list-style-type: none;
  float: left;
  margin: 0 30px;
  cursor: pointer;
}

#tabs ul li.Active a {
  color: #b32218;
}

#tabs {
  display: block;
}
Nathaniel Flick
  • 2,902
  • 2
  • 22
  • 31
1

I think you misunderstood the functionality of prev() a little bit, it won't get you the parent but following:

"Get the immediately preceding sibling..." (jQuery docs)

What you need is parent() , which will give you the parent of your clicked element (See docs) , combined with siblings() (Docs) it will give you the desired behavior of your tabs.

Furthermore, I've added outline: none to .#tabs ul li.Active a, to remove the default outline style for active tabs (If you want to keep it, just remove the commented line within the CSS).

See snippet below

$(document).ready(function() {
  $('.Tabs').tabs();

});

$('#tabs ul li a').click(function() {
  var $parent = $(this).parent();

  $parent.siblings().removeClass('Active');
  $parent.addClass('Active');
});
#tabs {
  border-top: 1px solid silver;
  border-bottom: 1px solid silver;
  border-right: white;
  background-color: #002750;
  color: white;
}

#tabs ul {
  padding: 6px 0 24px;
  margin: 0;
}

#tabs ul li {
  list-style-type: none;
  float: left;
  margin: 0 30px;
  cursor: pointer;
}

#tabs ul li.Active a {
  color: #b32218;
  outline: none; /* Remove this to leave the default outline functionality */
}

#tabs {
  display: block;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="Tabs">
  <div id="tabs">


    <ul>
      <li class="Active">
        <a href="#tab1">Information</a>
      </li>
      <li>
        <a href="#tab2">Accounting</a>
      </li>
      <li>
        <a href="#tab3">Tax Performance</a>
      </li>
    </ul>
  </div>
  <div id="tab1" class="Tab" style="display:none">
    <p>Tab 1 Content</p>
  </div>

  <div id="tab2" class="Tab" style="display:none">
    <p>Tab 2 Content</p>
  </div>

  <div id="tab3" class="Tab" style="display:none">
    <p>Tab 3 Content</p>
  </div>
</div>
Ramiz Wachtler
  • 5,623
  • 2
  • 28
  • 33