Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tomkel opened this issue Mar 3, 2016 · 34 comments
Closed

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

tomkel opened this issue Mar 3, 2016 · 34 comments
Labels

Comments

@tomkel
Copy link
Contributor

tomkel commented Mar 3, 2016

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>
@cvrebert
Copy link
Collaborator

cvrebert commented Mar 3, 2016

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

@tomkel
Copy link
Contributor Author

tomkel commented Mar 3, 2016

here are the v4 jsfiddles:
working
not working

@twbs-lmvtfy
Copy link

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.)

@twbs-lmvtfy
Copy link

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.)

@tomkel
Copy link
Contributor Author

tomkel commented Mar 3, 2016

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

@chris13524
Copy link

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>

@JimmyMultani
Copy link

JimmyMultani commented Apr 21, 2016

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

@RyanZim
Copy link

RyanZim commented May 6, 2016

@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?

@twbs-lmvtfy
Copy link

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.)

@RyanZim
Copy link

RyanZim commented May 6, 2016

Updated JSBin: http://jsbin.com/jinimoqoyo/1/edit?html,output

@CookingPixels
Copy link

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

@RyanZim
Copy link

RyanZim commented Jul 15, 2016

@cvrebert ping?

@CookingPixels
Copy link

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.

@VaclavElias
Copy link

VaclavElias commented Sep 7, 2016

I have got the same problem as @tomkel and @chris13524 mentioned.

I use this in Bootstrap 3, simple and clean:

  1. except no active class added to the button :) just to the tab-pane but the tab-content is updated on each click.
<div>
    <button data-toggle="tab" data-target="#page0" class="btn btn-default btn-sm">1</button>
    <button data-toggle="tab" data-target="#page1" class="btn btn-default btn-sm">2</button>
    <button data-toggle="tab" data-target="#page2" class="btn btn-default btn-sm">3</button>
</div>
<div class="tab-content">
    <div id="page0" class="tab-pane fade in active">Page 1</div>
    <div id="page1" class="tab-pane fade">Page 2</div>
    <div id="page2" class="tab-pane fade">Page 3</div>
</div>

Things have changed in Bootstrap 4. The above example

  1. adds the active class to the button on click
  2. it doesn't remove active class from siblings when you click the other button(s)
  3. the tab-content is only updated once on each click, then it doesn't update because of 2.

The issue is mentioned also here #20523.

@arctic-ice-cool
Copy link

arctic-ice-cool commented Nov 3, 2016

I have been using this as a workaround for now:

$(".nav-pills .nav-item .nav-link:not(.nav-pills .nav-item.dropdown .nav-link), .dropdown-item").click(function()
    {
        $(".dropdown-item.active").removeClass('active');
    });

@mdo
Copy link
Member

mdo commented Nov 28, 2016

Dupe of #18566.

@mdo mdo closed this as completed Nov 28, 2016
@mdo mdo mentioned this issue Nov 28, 2016
7 tasks
@ghost
Copy link

ghost commented Jan 3, 2018

I got around this by using onMouseUp instead of onClick because the active class is set on MouseDown. So using MouseUp means that my code won't execute until after the active class is set.

@patrickhlauke
Copy link
Member

I got around this by using onMouseUp instead of onClick

FWIW, this breaks it for keyboard users

@PORRIS
Copy link

PORRIS commented Jan 10, 2018

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/

@jaylahoti
Copy link

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"); }); });

@rob-lindman
Copy link

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

@fayazrehmani
Copy link

fayazrehmani commented Oct 8, 2018

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

@ahsan-asif
Copy link

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.

@javierpr0
Copy link

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.

@Tsanthan
Copy link

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

@kapilSoni101
Copy link

@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?

@damsalem
Copy link

damsalem commented Apr 7, 2020

@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')
})

@patrickhlauke
Copy link
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)

@zigojacko
Copy link

This problem still exists in Bootstrap 4.5.3...

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

@patrickhlauke
Copy link
Member

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

@zigojacko
Copy link

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 👍

@Yaz-X
Copy link

Yaz-X commented Jan 22, 2021

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');
                   });
          });

@kingofsevens
Copy link

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
Labels
Projects
None yet
Development

No branches or pull requests