Skip to content

class="active" managed improperly with data-toggle="tab" #19374

Description

@tomkel

This works:

<div class=navbar navbar-light bg-faded>
  <ul class=nav navbar-nav>
    <a class=nav-item nav-link active data-toggle=tab href=#start>Start</a>
    <a class=nav-item nav-link data-toggle=tab href=#form>Form</a>
    <a class=nav-item nav-link data-toggle=tab href=#status>Status</a>
    <a class=nav-item nav-link data-toggle=tab href=#reports>Reports</a>
  </ul>
</div>

<div class=tab-content>
  <div class=tab-pane active id=start>1</div>
  <div class=tab-pane id=form>2</div>
  <div class=tab-pane id=status>3</div>
  <div class=tab-pane id=reports>4</div>
</div>

However when the <ul> is changed to a <div>, the active classes are never unset, thus breaking the data-toggle.

This does not work:

<div class=navbar navbar-light bg-faded>
  <div class=nav navbar-nav>
    <a class=nav-item nav-link active data-toggle=tab href=#start>Start</a>
    <a class=nav-item nav-link data-toggle=tab href=#form>Form</a>
    <a class=nav-item nav-link data-toggle=tab href=#status>Status</a>
    <a class=nav-item nav-link data-toggle=tab href=#reports>Reports</a>
  </div>
</div>

<div class=tab-content>
  <div class=tab-pane active id=start>1</div>
  <div class=tab-pane id=form>2</div>
  <div class=tab-pane id=status>3</div>
  <div class=tab-pane id=reports>4</div>
</div>

Activity

  1. cvrebert commented on Mar 3, 2016

    @cvrebert
    Collaborator

    @tomkel Please make your example into a JS Bin or JS Fiddle.

  2. tomkel commented on Mar 3, 2016

    @tomkel
    ContributorAuthor

    here are the v4 jsfiddles:
    working
    not working

  3. twbs-lmvtfy commented on Mar 3, 2016

    @twbs-lmvtfy

    Hi @tomkel!

    You appear to have posted a live example (http://jsbin.com/fabijusugu/edit), which is always a good first step. However, according to Bootlint, your example has some Bootstrap usage errors, which might potentially be causing your issue:

    • line 21, column 5: W012: .navbar's first child element should always be either .container or .container-fluid

    You'll need to fix these errors and post a revised example before we can proceed further.
    Thanks!

    (Please note that this is a fully automated comment.)

  4. twbs-lmvtfy commented on Mar 3, 2016

    @twbs-lmvtfy

    Hi @tomkel!

    You appear to have posted a live example (http://jsbin.com/fixiyinuwi/edit), which is always a good first step. However, according to the HTML5 validator, your example has some validation errors, which might potentially be causing your issue:

    • line 23, column 9 thru column 76: Element a not allowed as child of element ul in this context. (Suppressing further errors from this subtree.)
    • line 24, column 9 thru column 68: Element a not allowed as child of element ul in this context. (Suppressing further errors from this subtree.)
    • line 25, column 9 thru column 70: Element a not allowed as child of element ul in this context. (Suppressing further errors from this subtree.)
    • line 26, column 9 thru column 71: Element a not allowed as child of element ul in this context. (Suppressing further errors from this subtree.)

    You'll need to fix these errors and post a revised example before we can proceed further.
    Thanks!

    (Please note that this is a fully automated comment.)

  5. tomkel commented on Mar 3, 2016

    @tomkel
    ContributorAuthor

    The bug persists when setting .navbar's first child to .container.

  6. chris13524 commented on Mar 28, 2016

    @chris13524

    I'm having this issue as well, although whatever I do, it does not work.

    <div class="nav nav-tabs" id="myTab">
        <a class="nav-item nav-link" data-toggle="tab" href="#one">One</a>
        <a class="nav-item nav-link" data-toggle="tab" href="#two">Two</a>
    </div>
    <div class="tab-content">
        <div class="tab-pane" id="one">ONE TAB</div>
        <div class="tab-pane" id="two">TWO TAB</div>
    </div>
    <script>
        $('#myTab a:first').tab('show');
    </script>
    
  7. JimmyMultani commented on Apr 21, 2016

    @JimmyMultani

    @cvrebert Any idea when this will be fixed? Thanks.

  8. RyanZim commented on May 6, 2016

    @RyanZim

    @cvrebert Here is an updated JSBin using the latest v4-dev: http://jsbin.com/jinimoqoyo/1/edit?html,output

    Also, could you please remove the awaiting reply label?

  9. twbs-lmvtfy commented on May 6, 2016

    @twbs-lmvtfy

    Hi @RyanZim!

    You appear to have posted a live example (http://jsbin.com/hedehalopa/1/edit), which is always a good first step. However, according to Bootlint, your example has some Bootstrap usage errors, which might potentially be causing your issue:

    • line 18, column 5: W012: .navbar's first child element should always be either .container or .container-fluid

    You'll need to fix these errors and post a revised example before we can proceed further.
    Thanks!

    (Please note that this is a fully automated comment.)

  10. RyanZim commented on May 6, 2016

    @RyanZim
  11. CookingPixels commented on Jul 15, 2016

    @CookingPixels

    This still happens, and it's tied with issue #18566

  12. RyanZim commented on Jul 15, 2016

    @RyanZim

    @cvrebert ping?

  13. CookingPixels commented on Jul 15, 2016

    @CookingPixels

    Sorry folks, I was reading an (still) out of date documentation on mdb (materia design bootstrap) that didn't match v4 and triggered this particular behaviour along with the .active class issue.

  14. 8 remaining items

  15. PORRIS commented on Jan 10, 2018

    @PORRIS

    in my case the problem was the bootstrap version, I was using version 4.0.0-alpha.6 and I was using a method from an old version

    1. add in the <ul role = "tablist">
    2. in the <li> the class = 'active' is no longer going, it is <li class = "nav-item">
    3. in the <a> aggregate <a class="nav-link active">
    4. in the divs of the tab-contet must go active
     <div class="tab-content">
           <div class="tab-pane slide-left active" id="slide1">
               .
               .
                .
            </div>
    </div>
    

    final structure in my case

    <ul class="nav nav-tabs nav-tabs-fillup" data-init-reponsive-tabs="dropdownfx" role="tablist">
     <li class="nav-item">
                <a data-toggle="tab" class="active nav-link " href="#slide1"><span>home</span></a>
            </li>
            <li class="nav-item">
                <a data-toggle="tab" class="nav-link" href="#slide2"><span>tab test</span></a>
            </li>
    </ul>
    <div class="tab-content">
     <div class="tab-pane slide-left active" id="slide1">
               .
               .
                .
            </div>
     <div class="tab-pane slide-left " id="slide2">
               .
               .
                .
            </div>
    </div>
    

    https://jsfiddle.net/tsathianathan/heyfshLy/

  16. jaylahoti commented on Jan 31, 2018

    @jaylahoti

    If you are using Bootstrap 3.3.7 then you can write below custom JQuery to fix this bug.

    $(function() { $(".apply-active").click(function() { // remove classes from all $(".apply-active").removeClass("active"); // add class to the one we clicked $(this).addClass("active"); }); });

  17. rob-lindman commented on Jun 16, 2018

    @rob-lindman

    @jaylahoti I tried this and it seems to disable the tabs working at all.

  18. fayazrehmani commented on Oct 8, 2018

    @fayazrehmani

    fixed my issue through remove bt functionality with little custom code
    $('#containerContainingTabs a').on('click', function(e) {
    e.preventDefault();
    $(this).tab('show');
    var theThis = $(this);
    $('#containerContainingTabs a').removeClass('active');
    theThis.addClass('active');
    });

    #containerContainingTabs is the id on div which container tabs links

  19. ahsan-asif commented on Apr 18, 2019

    @ahsan-asif

    Thank you so much fayazrehmani for this code...Its really work for me. I'm stuck before a 6h ago but you really solved my problem to give me this code.

  20. javierpr0 commented on Jun 25, 2019

    @javierpr0

    fixed my issue through remove bt functionality with little custom code
    $('#containerContainingTabs a').on('click', function(e) {
    e.preventDefault();
    $(this).tab('show');
    var theThis = $(this);
    $('#containerContainingTabs a').removeClass('active');
    theThis.addClass('active');
    });

    #containerContainingTabs is the id on div which container tabs links

    Thank you @fayazrehmani this work for me.

  21. Tsanthan commented on Feb 10, 2020

    @Tsanthan

    $('.navbar-nav .nav-link').click(function(){
    $('.navbar-nav .nav-link').removeClass('active');
    $(this).addClass('active');
    })

  22. kapilSoni101 commented on Apr 4, 2020

    @kapilSoni101

    @PORRIS :sir but in my case whole page is redirected after click any tab can you tell me what the problem and i am angular9?

  23. damsalem commented on Apr 7, 2020

    @damsalem

    @PORRIS :sir but in my case whole page is redirected after click any tab can you tell me what the problem and i am angular9?

    You need to use e.preventDefault() exactly as the Bootstrap Documentation stated:

    $('#myTab a').on('click', function (e) {
      e.preventDefault() // <-- this line
      $(this).tab('show')
    })
    
  24. patrickhlauke commented on Apr 7, 2020

    @patrickhlauke
    Member

    and really, for in-page controls, you should be using <button> elements rather than links (unless that's something in angular that can't be changed for some odd reason)

  25. zigojacko commented on Nov 18, 2020

    @zigojacko

    This problem still exists in Bootstrap 4.5.3...

    The 'active' class is not removed from default tab when switching between tabs.

  26. patrickhlauke commented on Nov 18, 2020

    @patrickhlauke
    Member

    can i suggest making a fresh issue, with a fresh codepen/jsbin example?

  27. zigojacko commented on Nov 18, 2020

    @zigojacko

    can i suggest making a fresh issue, with a fresh codepen/jsbin example?

    I would normally but there are countless issues of the same problem on here spanning a number of years and it still hasn't been addressed despite all with activity and closed as duplicates etc.

    If I find the time at some point, I'll try and raise a new issue 👍

  28. Yaz-X commented on Jan 22, 2021

    @Yaz-X

    2021 and still an issue LOL, i have multiple tab controls in the page, so this worked for me:

    $(document).ready(function () {
                 //Fix Bootstrap bug with Tab control not removing active class when switching tabs
                  $(".nav-tabs li.nav-item a.nav-link").click(function () {
                            $(".nav-tabs li.nav-item a.nav-link").removeClass('active').off('click');
                       });
              });
    
  29. kingofsevens commented on Feb 15, 2021

    @kingofsevens

    Here is a working example with a fix.

    Below is the fix. If you comment out that part, you can still see it is not fixed for 5.0.0-beta2

    $(".nav-pills button").click(function () {
      $(this).closest(".nav-pills").find("button").not($(this)).removeClass("active");
    });
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions