ProblemChild ProblemChild - 3 months ago 19
Javascript Question

How do I add 'active' class dynamically for Bootstrap 3 nav-tabs?

I am using Bootstrap 3 with asp.net Web Forms with Master Page. In my Master Page I have vertical

nav-tabs
on the right of the page which slide when clicking a button. Within those individual
nav-tabs
I have default horizontal Bootstrap
nav-tabs
. My problem is, I want to dynamically add/remove the active class using using Javascript.

In the vertical tabs I have removed the
class="active"
initially and want to add it to the individual vertical nav tab on clicking it. And when I click on it again, it should return to its original color.

Here's the Master Page code :

<div class="tab-pane tabPanes" id="rightTab1">
<div>
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">
<a href="#rightTab1_1" aria-controls="home" role="tab" data-toggle="tab">Home</a>
</li>

<li role="presentation">
<a href="#rightTab1_2" aria-controls="profile" role="tab" data-toggle="tab">Profile</a>
</li>
</ul>
</div>
</div>

<div class="tab-pane tabPanes" id="rightTab2">
<div>
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">
<a href="#rightTab2_1" aria-controls="home" role="tab" data-toggle="tab">Home</a>
</li>

<li role="presentation">
<a href="#rightTab2_2" aria-controls="profile" role="tab" data-toggle="tab">Profile</a>
</li>
</ul>
</div>
</div>

<div class="row">
<ul class="nav nav-tabs tabs-right sideways">
<li id="nav1"><a href="#rightTab1" data-toggle="tab">Tab1</a></li>
<li id="nav2"><a href="#rightTab2" data-toggle="tab">Tab2</a></li>
</ul>
</div>


My Javascript :

$(document).ready(function () {
$("#rightTab1").hide();
$("#rightTab2").hide();
$("#rightTab3").hide();

$("#nav1").click(function () {
if ($("#rightTab1").is(":hidden") && $("#rightTab2").is(":visible")) {
$("#rightTab1").delay(1000).show('slide', { direction: 'right' }, 500);
$("#rightTab2").hide('slide', { direction: 'right' }, 500);
}

else if ($("#rightTab1").is(":hidden") && $("#rightTab2").is(":hidden")) {
$("#rightTab1").show('slide', { direction: 'right' }, 500);
$("#rightTab2").hide('slide', { direction: 'right' }, 500);
}

else {
$("#rightTab1").hide('slide', { direction: 'right' }, 500);
$(".sideways li a").removeClass('active');
}
});
});


Please help me. Thanks in advance.

Answer

Use href in click and it will work fine see this example no need to write jquery it self

$('[data-toggle^=pill]').click(function(e) {


  if ($(this).parent().hasClass('active')) {
    console.log($(this).parent().removeClass('active'))
    console.log($($(this).attr('href')).removeClass('in active'))

  } else {
    $('[data-toggle^=pill]').parent().removeClass('active')
    $('[class^=tab-pane]').removeClass('in active') 

    console.log($(this).parent().addClass('active'))
    console.log($($(this).attr('href')).addClass('in active'))
  }

  e.stopPropagation();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Case</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
</head>

<body>

  <div class="container">
    <h2>Dynamic Pills</h2>
    <ul class="nav nav-pills">
      <li><a data-toggle="pill" href="#home">Home</a>
      </li>
      <li><a data-toggle="pill" href="#menu1">Menu 1</a>
      </li>
      <li><a data-toggle="pill" href="#menu2">Menu 2</a>
      </li>
      <li><a data-toggle="pill" href="#menu3">Menu 3</a>
      </li>
    </ul>

    <div class="tab-content">
      <div id="home" class="tab-pane fade">
        <h3>HOME</h3>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
      </div>
      <div id="menu1" class="tab-pane fade">
        <h3>Menu 1</h3>
        <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
      </div>
      <div id="menu2" class="tab-pane fade">
        <h3>Menu 2</h3>
        <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam.</p>
      </div>
      <div id="menu3" class="tab-pane fade">
        <h3>Menu 3</h3>
        <p>Eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</p>
      </div>
    </div>
  </div>

</body>

</html>

Comments