18

Is there a way to disable and gray out the checkbox label as well once the checkbox becomes disabled using Bootstrap and Jquery?

<div class="checkbox">        
    <input id="accept" name="accept" type="checkbox" value="True">
    <label for="accept" class="control-label">Incremental</label>
</div>

I am now using the bellow code to disable the checkbox:

 $("#accept").prop("disabled", true);
Tushar Gupta - curioustushar
  • 56,454
  • 22
  • 99
  • 107
alloyoussef
  • 737
  • 3
  • 9
  • 22

5 Answers5

25

You can do it with CSS only

$("#accept").prop("disabled", true);
input[type=checkbox][disabled] + label {
    color: #ccc;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="checkbox">
    <input id="accept" name="accept" type="checkbox" value="True">
    <label for="accept" class="control-label">Incremental</label>
</div>

Read http://css-tricks.com/almanac/selectors/c/checked/

Attribute selectors in CSS

dota2pro
  • 6,421
  • 7
  • 34
  • 68
Tushar Gupta - curioustushar
  • 56,454
  • 22
  • 99
  • 107
  • 5
    For everyone like me that have no idea what the + does and wondering why it is not working. The label element needs to be directly after the input element (not before). – Kristjan Liiva Nov 25 '15 at 11:49
  • And how is the css impacted if teh label is before the checkbox? – Chad Aug 22 '16 at 20:05
2

Seems that setting the word disabled in the div "class" as well as in the input will do it.

<div class="checkbox disabled">
  <label><input type="checkbox" value="" disabled>check Me</label>
</div>
reddragon72
  • 191
  • 2
  • 16
1

Bootstrap dose not provide such type facility, you can manage from your jQuery custom code

if($("#accept").has("[disabled]")){
  $("#accept").parent().find("label").css("color", "#dadada");
}
Girish
  • 11,607
  • 3
  • 35
  • 49
-2

Try using:

$("#accept").addClass("disabled");
Venom
  • 321
  • 2
  • 4
-2
  $('label.control-label').addClass('disabled');

to disable as a button

 $('label.control-label').addClass('btn-disabled');
Bellash
  • 6,659
  • 6
  • 45
  • 82