Can you disable tabs in Bootstrap?
Asked Answered
S

17

143

Can you disable tabs in Bootstrap 2.0 like you can disable buttons?

Sufferance answered 11/2, 2012 at 2:29 Comment(0)
A
200

You could remove the data-toggle="tab" attribute from the tab as it's hooked up using live/delegate events

Amherst answered 11/2, 2012 at 2:35 Comment(9)
Thanks worked a treat, also added css "cursor:no-drop;" for cursor so use knows why they can't click itSufferance
cursor: not-allowed; is more appropriate in this case. Unless you are actually drag-and-dropping.Mckinley
This helped me, thank you. To re-activate the tab just add the data-toggle attribute back to the <a> tag, child of the <li> of interest: $('#my-tab-li-element').children('a').attr('data-toggle', 'tab');Made
Or add a disabled class to the liExterritorial
+ Adding a disabled css-class to the liMonda
You need to use both suggestions above: Add "disabled" class to <li> AND Remove data-toogle or href attribute from tabBungle
I simply added this CSS and now class="disabled" works as expected .nav.nav-tabs > li.disabled { pointer-events: none; a { color: silver; } }Printable
The border of the currently active tab gets still removed with this solution, when I click on a disabled tab.Steffens
@Printable super answer. Best solutions. It disables the click and allows us to navigate through next previous buttons. Where as the above solutions disables all the options to navigate.Batory
U
51

As of 2.1, from bootstrap documentation at http://twitter.github.com/bootstrap/components.html#navs, you can.

Disabled state

For any nav component (tabs, pills, or list), add .disabled for gray links and no hover effects. Links will remain clickable, however, unless you remove the href attribute. Alternatively, you could implement custom JavaScript to prevent those clicks.

See https://github.com/twitter/bootstrap/issues/2764 for the feature add discussion.

Unseasoned answered 7/12, 2012 at 22:10 Comment(4)
this is one of the main functionalities and it boggles me it is not yet implementedComplication
It is implemented in v3Roborant
Yes but links are still clickable.Umbles
Exactly, the viable solution so far is to remove the data-toggle attribute.Jab
W
37

I added the following Javascript to prevent clicks on disabled links:

$(".nav-tabs a[data-toggle=tab]").on("click", function(e) {
  if ($(this).hasClass("disabled")) {
    e.preventDefault();
    return false;
  }
});
Wifely answered 4/12, 2013 at 15:28 Comment(3)
I think a combination of this and @hotzu's response should be the answer. You should add the disabled class to the li element and then add the javascript you specified except the condition you'd be checking against would be: if ($(this).parent().hasClass('disabled')) {..}.Herbarium
@im1dermike I don't see why I would be doing this?Wifely
Old question, but it's what I found so I'm adding this. You would do that check because the li is what changes the visuals for the user and is consequently what should have the disabled class.Whisper
M
27

i think the best solution is disabling with css. You define a new class and you turn off the mouse events on it:

.disabledTab{
    pointer-events: none;
}

And then you assign this class to the desired li element:

<li class="disabled disabledTab"><a href="#"> .... </a></li>

You can add/remove the class with jQuery also. For example, to disable all tabs:

$("ul.nav li").removeClass('active').addClass('disabledTab');

Here is an example: jsFiddle

Moorehead answered 17/3, 2014 at 9:22 Comment(2)
i would strongly recommend against that. since a css only solution leaves the tabs clickable. in case there are other events listening for those clicks, they will be executed, which is not the desired result. (i guess?)Sulfide
In my case, this did the trick. I built a form consisting of multiple tabs. Going to the next tab is done via a button, which triggers a click-event on the tab to go to. So disabling clicks completely is not an option for me, but removing pointer events from the tabs was just what I needed.Lancewood
S
20

No Need Any Jquery, Just One Line CSS

.nav-tabs li.disabled a {
    pointer-events: none;
}
Sardonyx answered 10/2, 2017 at 4:41 Comment(1)
I had to do this on li.disabled (not li.disabled a)Lenoir
Q
11

Also, I'm using following solution:

$('a[data-toggle="tab"]').on('click', function(){
  if ($(this).parent('li').hasClass('disabled')) {
    return false;
  };
});

Now you just adding class 'disabled' to the parent li and tab doesn't work and become gray.

Quimper answered 26/1, 2015 at 13:51 Comment(0)
C
6

Old question but it kind of pointed me in the right direction. The method I went for was to add the disabled class to the li and then added the following code to my Javascript file.

$('.nav-tabs li.disabled > a[data-toggle=tab]').on('click', function(e) {
    e.stopImmediatePropagation();
});

This will disable any link where the li has a class of disabled. Kind of similar to totas's answer but it won't run the if every time a user clicks any tab link and it doesn't use return false.

Hopefully it'll be useful to someone!

Caddoan answered 28/8, 2014 at 10:4 Comment(1)
good! but need e.preventDefault() before e.stopImmediatePropagation()Lanna
J
6

For my use, the best solution was a mix of some of the answers here, which are :

  • Adding the disabled class to the li I want to disable
  • Add this piece of JS :
$(".nav .disabled>a").on("click", function(e) {
    e.preventDefault();
    return false;
});
  • You can even remove the data-toggle="tab" attribute if you want Bootstrap to not interfer at all with your code.

NOTE: The JS code here is important, even if you remove the data-toggle because otherwise, it will update your URL by adding the #your-id value to it, which is not recommended because your tab is disabled, thus should not be accessed.

Jab answered 29/9, 2014 at 13:4 Comment(0)
P
5

With only css, you can define two css classes.

<style type="text/css">
    /* Over the pointer-events:none, set the cursor to not-allowed.
    On this way you will have a more user friendly cursor. */
    .disabledTab {
        cursor: not-allowed;
    }
    /* Clicks are not permitted and change the opacity. */
    li.disabledTab > a[data-toggle="tab"] {
        pointer-events: none;
        filter: alpha(opacity=65);
        -webkit-box-shadow: none;
        box-shadow: none;
        opacity: .65;
    }
</style>

This is an html template. The only thing needed is to set the class to your preferred list item.

<ul class="nav nav-tabs tab-header">
    <li>
        <a href="#tab-info" data-toggle="tab">Info</a>
    </li>
    <li class="disabledTab">
        <a href="#tab-date" data-toggle="tab">Date</a>
    </li>
    <li>
        <a href="#tab-photo" data-toggle="tab">Photo</a>
    </li>
</ul>
<div class="tab-content">
    <div class="tab-pane active" id="tab-info">Info</div>
    <div class="tab-pane active" id="tab-date">Date</div>
    <div class="tab-pane active" id="tab-photo">Photo</div>
</div>
Prelacy answered 11/1, 2017 at 11:56 Comment(0)
S
2

Suppose, this is your TAB and you want to disable it

<li class="" id="groups"><a data-toggle="tab" class="navuserli" href="#groups" aria-expanded="false">Groups</a></li>

So you can also disable this tab by adding dynamic css

$('#groups').css('pointer-events', 'none')
Stedman answered 7/3, 2017 at 10:55 Comment(0)
P
1

In addition to James's answer:

If you need to disable the link use

$('a[data-toggle="tab"]').addClass('disabled');

If you need to prevent a disabled link from loading the tab

$('a[data-toggle="tab"]').click(function(e){

            if($this.hasClass("disabled")){

                e.preventDefault();

                e.stopPropagation();

                e.stopImmediatePropagation();

                return false;

            }
}

If you need to unable the link

$('a[data-toggle="tab"]').removeClass('disabled');
Pya answered 7/5, 2016 at 17:46 Comment(0)
S
1

You can disable a tab in bootstrap 4 by adding class disabled to the child of nav-item as follows

<li class="nav-item">
    <a class="nav-link disabled" data-toggle="tab" href="#messages7" role="tab" aria-expanded="false">
        <i class="icofont icofont-ui-message"></i>Home</a>
    <div class="slide"></div>
</li>
Shotten answered 29/9, 2019 at 13:53 Comment(0)
D
0

I tried all suggested answers, but finally i made it work like this

if (false) //your condition
{
    $("a[data-toggle='tab'").prop('disabled', true);
    $("a[data-toggle='tab'").each(function () {
        $(this).prop('data-href', $(this).attr('href')); // hold you original href
        $(this).attr('href', '#'); // clear href
    });                
    $("a[data-toggle='tab'").addClass('disabled-link');
}
else
{
    $("a[data-toggle='tab'").prop('disabled', false);
    $("a[data-toggle='tab'").each(function () {
        $(this).attr('href', $(this).prop('data-href')); // restore original href
    });
    $("a[data-toggle='tab'").removeClass('disabled-link');
}
// if you want to show extra messages that the tab is disabled for a reason
$("a[data-toggle='tab'").click(function(){
   alert('Tab is disabled for a reason');
});
Delozier answered 13/9, 2014 at 12:27 Comment(0)
H
0

None of the answers work for me. Remove data-toggle="tab" from the a prevents the tab from activating, but it also adds the #tabId hash to the URL. That is unacceptable to me. What is also unacceptable is using javascript.

What does work is added the disabled class to the li and removing the href attribute of its containing a.

Herbarium answered 6/11, 2014 at 18:57 Comment(1)
Now that I read down further, this is also basically the same exact answer as the one from @Scott Stafford, who answered it 2 years earlier...Tref
F
0

my tabs were in panels, so i added a class='disabled' to the tabs anchor

in javascript i added:

$(selector + ' a[data-toggle="tab"]').on('show.bs.tab', function (e) {
    if ($(this).hasClass('disabled')){
        e.preventDefault();
        return false;
    }
})

and for presentation in less i added:

.panel-heading{
    display:table;
    width:100%;
    padding-bottom:10px;
    ul.nav-tabs{
        display:table-cell;
        vertical-align:bottom;
        a.disabled{
            .text-muted;
            cursor:default;
            &:hover{
                background-color:transparent;
                border:none;
            }
        }
    }
}
Fruiterer answered 2/8, 2016 at 5:3 Comment(0)
M
0

Most easy and clean solution to avoid this is adding onclick="return false;" to a tag.

<ul class="nav nav-tabs">
    <li class="active">
        <a href="#home" onclick="return false;">Home</a>
    </li>    
    <li>
        <a href="#ApprovalDetails" onclick="return false;">Approval Details</a>
    </li>
</ul>
  • Adding "cursor:no-drop;" just makes cursor look disabled, but is clickable, Url gets appending with href target for ex page.apsx#Home
  • No need of adding "disabled" class to <li> AND removing href
Migraine answered 30/8, 2016 at 10:16 Comment(0)
V
-1

Here's my attempt. To disable a tab:

  1. Add "disabled" class to tab's LI;
  2. Remove 'data-toggle' attribute from LI > A;
  3. Suppress 'click' event on LI > A.

Code:

var toggleTabs = function(state) {
    disabledTabs = ['#tab2', '#tab3'];
    $.each(disabledTabs, $.proxy(function(idx, tabSelector) {
        tab = $(tabSelector);
        if (tab.length) {
            if (state) {
                // Enable tab click.
                $(tab).toggleClass('disabled', false);
                $('a', tab).attr('data-toggle', 'tab').off('click');
            } else {
                // Disable tab click.
                $(tab).toggleClass('disabled', true);
                $('a', tab).removeAttr('data-toggle').on('click', function(e){
                    e.preventDefault();
                });
            }
        }
    }, this));
};

toggleTabs.call(myTabContainer, true);
Vinaigrette answered 30/10, 2015 at 0:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.