36

I use markup to display a dropdown menu using Twitter Bootstrap.

<ul class="nav pull-right">
    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Menu <b class="caret"></b></a>
        <ul class="dropdown-menu">
            <li><a href="#">Menu item 1...</a></li>
            <li class="divider"></li>
            <li><a href="#">Menu item 2...</a></li>
            <li><a href="#">Menu item 3</a></li>
        </ul>
    </li>
</ul>

I want to be able to make menu items appear disabled, i.e. no hover effect and probably a different text decoration to make the disabled state visible to the user.

What is the best way to accomplish this? Is there an exisisting bootstrap css class I can add to the <li> or <a> element?

4
  • 1
    You want the link to "appear" disabled but not actually disabled? Commented Mar 13, 2012 at 12:54
  • Sorry if the question was unclear. I want one or more menu items to be disabled (in every way, not only appearance). Commented Mar 13, 2012 at 13:19
  • And how are you expecting the events to take place, e.g. you want the buttons to become enabled if a certain action is taken? Or you're just looking for a way to easily disable the links with some jQuery? You can't fully disable a link with plain css. Commented Mar 13, 2012 at 13:33
  • It doesn't matter if it is done using jQuery or css. Commented Mar 13, 2012 at 13:54

8 Answers 8

78

When outputing the code for a disabled drop down, why not simply use :

<li class='disabled'>
  <a href="#">Menu</a>
</li>

You can always add the caret back if you still want it to appear.

Update:
Adding W3Schools Link

Sign up to request clarification or add additional context in comments.

5 Comments

+1 The documentation shows you this twitter.github.com/bootstrap/components.html#navs (under "Disabled state")
Also from the documentation, if you don't want links to be clickable then get rid of the href. Although retain the a for formatting.
Good answer. The only thing I don't like about this is that Bootstrap allows the dropdown to close when the user clicks on the disabled item. Hm...
When I tried this, the link "looked" disabled, but it could still be clicked on and used for navigation
This is a great bootstrap style, but it has one problem, it doesn't actually stop the item from being clicked. @buzzy answer below takes care of this.
22

You can attach a custom disabled class to your menu link a tag that you want to disable and simply remove the default action by using preventDefault and targetting that class, like so:

$(".disabled-link").click(function(event) {
  event.preventDefault();
});

Then you can style all events from the .disabled-link with a grey backdrop or any style you like;

CSS

a.disabled-link,
a.disabled-link:visited ,
a.disabled-link:active,
a.disabled-link:hover {
    background-color:#d9d9d9 !important;
    color:#aaa !important;
}

Demo

6 Comments

Thanks, that's just what I needed. I did some googling and found out about the pointer-events css attribute. This updated version of your jsfiddle uses pointer-events instead of using script to disable the action: jsfiddle.net/fABRr/2
Sadly that is only supported by FF4+ and Chrome (last time i checked) :( , thought about using the same as well but did not think it was a proper answer with such lack of support.
Support Chart, no IE support (no surprise there), FF is supported ver 3.6+, no Opera support, Chrome ver 16+ support. Not bad, but not so good either :(
Ouch. I guess I'll check for the disabled-link class in the click handlers for the menu items then.
When combining bootstrap with some frameworks (eg. knockout), preventing the click event may not work. This is for example the case using knockout's data-bind="click: ..." . Using .unbind("click"); instead will do the trick.
|
7

I prefer this (LESS):

/* Disable link */
a.disabled, 
a.disabled:visited ,
a.disabled:active,
a.disabled:hover {  
  color: #999 ;
  cursor: default;
}
.dropdown-menu {
  a.disabled, 
  a.disabled:visited ,
  a.disabled:active,
  a.disabled:hover {  
    color: #999 ;
    cursor: default;
    background-color: white;
  }
}

Comments

6

To disable the the dropdown use:

$('.dropdown-toggle').addClass('disabled');

To enable it back:

$('.dropdown-toggle').removeClass('disabled');

2 Comments

For some reason, it doesn't add/remove after the initial page load.
it looks disabled but is still clickable. I've added .dropdown-item.disabled {pointer-events: none;} as mentioned in comments here
6

YES, Bootstrap has a predefined class with all necessary styling you need. You can simply add disabled class to whichever <li> you want

2 Comments

This does not actually work, at least with Bootstrap 3.x.
Oh waid, you said for <li> - that works. Make sure you don't add it for <a>.
3

Just to add to Andres answer (don't have enough reputation to add comments :( ). You need to return false from the event handler or it might continue executing other handlers.

$(".disabled-link").click(function(event) {
  event.preventDefault();
  return false;
});

5 Comments

I believe event.preventDefault() takes care of that.
@codeape I believe that return false is needed for IE though.
@codeape In Chrome, return false is mandatory as well, so I would say it can be considered a good practice.
In a jQuery event handler, return false does two things: event.stopPropagation() and event.preventDefault(). (When not using jQuery, return false in event handlers means other things depending on where you use it...) So if you add return false, you can remove event.preventDefault(), because that will be done anyway.
If you add return false; do not disappear the dropdown if it triggers a confirmation dialog.
1

Similar to above you can use:

    li.disabled > a {
    color:#aaa !important;
    }

This way you are keeping the same bootstrap default class for disabled links and implement the preventDefault() Javascript to disabled the link.

$(".disabled").click(function(event) {
event.preventDefault();
return false;
});

Comments

0
<div class="dropdown-menu">
  <a class="dropdown-item" href="#">Regular link</a>
  **<a class="dropdown-item disabled" href="#">Disabled link</a>**
  <a class="dropdown-item" href="#">Another link</a>
</div>

Add .disabled to items in the dropdown to style them as disabled. Source: www.getbootstrap.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.