Disable Twitter Bootstrap Button Group
Asked Answered
S

3

9

I have a button group on a page that is used for a selection. After the selection is made I want the button group to still be visible so the user can see the selection they made, but I don't want them to be able to use it anymore. Is there a way to disable to the button group?

<div class="btn-group-vertical">
    <button type="button" class="btn btn-primary btn-lg">Option 1</button>
    <button type="button" class="btn btn-primary btn-lg">Option 2</button>
    <button type="button" class="btn btn-primary btn-lg">Option 3</button>
</div>
Shaunta answered 7/5, 2014 at 12:52 Comment(3)
You can do this through javascript or jquery, but I'm curious why? What if people change their mind and want to select another?Fluoroscope
@Fluoroscope Could you please give an example how to do it using javascript or jquery? Can't it be done by bootstrap tools? It may be useful if you want to make read only forms.Siemens
@baurzhan Bootstrap's JS tooling is jQueryEspinosa
L
8

You can use the disabled attribute on the buttons to disable their use, as described in the Bootstrap docs. It essentially involves adding the attribute 'disabled' to the button element, like so;

<div class="btn-group-vertical">
    <button type="button" class="btn btn-primary btn-lg" disabled="disabled">Option 1</button>
    <button type="button" class="btn btn-primary btn-lg" disabled="disabled">Option 2</button>
    <button type="button" class="btn btn-primary btn-lg" disabled="disabled">Option 3</button>
</div>

You can use jQuery to dynamically add this attribute once you've done whatever you want to do

$('.btn-group-vertical button').attr('disabled','disabled');

And if you wanted to re-enable it

$('.btn-group-vertical button').removeAttr('disabled');

Bear in mind that the above would apply this behaviour to all button groups with this class name. If you don't want that you'll need to add an ID to the btn group div and use that in the jQuery selector.

Louralourdes answered 7/5, 2014 at 12:59 Comment(4)
This does disable the buttons, but I am looking for a way to keep the styling as well so the user can see the button they selected.Shaunta
@Shaunta did you ever get to a solution for this? Because I am looking for the same thing.Megalith
@Megalith and thraxst Try adding this to your css: .btn-primary.active[disabled] { background-color: #e6e6e6 !important; }Lowspirited
what if insted of btn-group of buttons you use a div that defines btn btn-group and also uses data-toggle="buttons" then use labels as actual buttons. when disabling set overall div that does toggle to attr('data-toggle','off') that seems to work and keeps colours you preset over itInterlinear
M
3

I wanted to keep the styling too, So I left out the disabled field and just counter-acted the CSS with a new class display-only:

<div class='btn-group display-only' data-toggle='buttons'>      
    <label class='btn btn-default button-array'>Friday PM</label>
    <label class='btn btn-default button-array'>Saturday AM</label>
    <label class='btn btn-default button-array'>Graduation</label>
    <label class='btn btn-default button-array'>Saturday PM</label>
    <label class='btn btn-default button-array'>Sunday AM</label>
    <label class='btn btn-default button-array'>Sunday PM</label>
    <label class='btn btn-default button-array'>Monday AM</label>
</div> 

And the CSS:

.btn-group.display-only label.btn {
    pointer-events: none;
    cursor: not-allowed;
}
.btn-group.display-only label.btn.active {
    opacity: 1;
    color: #333;
    background-color: #e6e6e6;
    border-color: #adadad;webkit-box-shadow: inset 0 3px 5px rgba(0,0,0,.125);
    box-shadow: inset 0 3px 5px rgba(0,0,0,.125);
}

..Seems to work well for me.

Megalith answered 26/11, 2015 at 10:36 Comment(0)
V
1

Before v4.0.0, Bootstrap Toggle buttons do not honor .disabled or [disabled] :

https://github.com/twbs/bootstrap/issues/21237

Vorous answered 14/8, 2018 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.