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
  • 58,085
  • 24
  • 103
  • 107
alloyoussef
  • 757
  • 3
  • 10
  • 24

5 Answers5

26

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
  • 7,220
  • 7
  • 44
  • 79
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 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
  • 1
  • 3
  • 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,907
  • 3
  • 34
  • 51
-2

Try using:

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

to disable as a button

 $('label.control-label').addClass('btn-disabled');
Bellash
  • 7,560
  • 6
  • 53
  • 86