1

I have two chech Boxes (one of them disabled). I have replaced the check boxes with a div with background color with toggle function so that checked = green, unchecked = red.

HTML

<input type="checkbox" class="input_class_checkbox" disabled="disabled">    
<br/>
<input type="checkbox" class="input_class_checkbox">

JQUERY

$('.input_class_checkbox').each(function(){
$(this).hide().after('<div class="class_checkbox" />');

});

$('.class_checkbox').on('click',function(){
$(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked'))
});

CSS

.class_checkbox {
width: 20px;  
height: 20px;
background-color: red;
}

.class_checkbox.checked {
  background-color: green;
}

 .class_checkbox.disabled {
  background-color: blue;
}

How do I apply this .disabled CSS on the disabled check box on page load? also check boxes can be disabled programmatically, so can I implement onDisabled kind of Jquery code to change the appearance when a check box is disabled?

6
  • 1
    You're kind of overthinking it. Just toggle between two classes. Something like "checkbox_enabled" and "checkbox_disabled," rather than try to coax an HTML attribute into doing something it wasn't exactly designed to do. Commented May 22, 2015 at 10:34
  • just select all things that are classed so $('.disabled').css( .... Commented May 22, 2015 at 10:34
  • @thisOneGuy except the OP hasn't really created a disabled class. He's trying to hang this functionality off the disabled HTML attribute. Commented May 22, 2015 at 10:36
  • And for a more practical JQ code example, maybe something like: Commented May 22, 2015 at 10:37
  • BuggyCoder, do you have two states here, or three? Looks like you're going for red, green, and blue. Commented May 22, 2015 at 10:44

3 Answers 3

3

I think you want something like:

$( "input:disabled" ).next().css( "background-color", "yellow" );

Check Fiddle

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

Comments

1

Dont use Javascript*

You should ideally look to use HTML and CSS for this, without reliance on Javascript. This ensures correct separation of concerns (HTML = content, CSS = styling, JS = functionality). It is also a cleaner more performant solution.

input+span {
  height: 10px;
  width: 10px;
  display: inline-block;
  border: 1px solid;
}
input {
  display: none;
}
input:checked +span {
  background: lightgreen;
}
input:disabled + span {
  background: lightgrey;
}
<label>
  <input type="checkbox" />
  <span></span>
</label>

<label>
  <input type="checkbox" disabled />
  <span></span>
</label>

*Unless unavoidable

Comments

0

You need to write :disabled

Something like:

 .input_class_checkbox:disabled {
  background-color: blue;
}

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.