Reputation: 2014
I'm composing a stringbuilder on serverside (HTML code) and by ajax sending a response to jquery function that puts this HTML in place. On serverside is decided which checkbox will be checked. So far so good.
I would like to select labels for checked checkboxes and change colour so checked countries would be differently coloured. My trouble is that I can't select desired labels and change color property. I tried with this what you see and of course using classes and +
and >
and :before
and other weird things...
So, how to select them and change any property on the label?
I would like to do it using CSS, I know how to do it using jquery but it seems so wrong.
label + input.chkCountry[type="checkbox"]:checked {color:green;}
<ul class="chkbox">
<li><label class="lblCountry" for="chkCC_AF"><input type="checkbox" class="chkCountry" id="chkCC_AF" name="chk_AF" value="AF" checked="checked">Afghanistan</label></li>
<li><label class="lblCountry" for="chkCC_AL"><input type="checkbox" class="chkCountry" id="chkCC_AL" name="chk_AL" value="AL">Albania</label></li>
<li><label class="lblCountry" for="chkCC_DZ"><input type="checkbox" class="chkCountry" id="chkCC_DZ" name="chk_DZ" value="DZ">Algeria</label></li>
<li><label class="lblCountry" for="chkCC_AS"><input type="checkbox" class="chkCountry" id="chkCC_AS" name="chk_AS" value="AS">American Samoa</label></li>
</ul>
Upvotes: 4
Views: 15038
Reputation: 7661
You only need to change the HTML and selector. In CSS the label doesn't know if the checkbox is checked so you have to turn it around.
input.chkCountry[type="checkbox"]:checked + label {color:green;}
/*input:checked + label {color:green;} /* Short selector */
<ul class="chkbox">
<li>
<input type="checkbox" class="chkCountry" id="chkCC_AF" name="chk_AF" value="AF" checked="checked">
<label class="lblCountry" for="chkCC_AF">Afghanistan</label>
</li>
<li>
<input type="checkbox" class="chkCountry" id="chkCC_AL" name="chk_AL" value="AL">
<label class="lblCountry" for="chkCC_AL">Albania</label>
</li>
<li>
<input type="checkbox" class="chkCountry" id="chkCC_DZ" name="chk_DZ" value="DZ">
<label class="lblCountry" for="chkCC_DZ">Algeria</label>
</li>
<li><input type="checkbox" class="chkCountry" id="chkCC_AS" name="chk_AS" value="AS">
<label class="lblCountry" for="chkCC_AS">American Samoa</label>
</li>
</ul>
Upvotes: 14
Reputation: 1428
Follow below code example
input[type="checkbox"]:checked ~ label {
color: green;
}
<ul class="chkbox">
<li>
<input type="checkbox" id="chkCC_AF" name="chk_AF" value="AF" checked="checked">
<label class="lblCountry" for="chkCC_AF">Afghanistan</label>
</li>
<li>
<input type="checkbox" id="chkCC_AL" name="chk_AL" value="AL">
<label class="lblCountry" for="chkCC_AL">Albania</label>
</li>
</ul>
You will need to move label after checkbox in your HTML.
Upvotes: 3
Reputation: 3117
Try This way:
.chkCountry:checked + .lblCountry {
color: green;
}
<ul class="chkbox">
<li>
<input type="checkbox" class="chkCountry" id="chkCC_AF" name="chk_AF" value="AF" checked="checked">
<label class="lblCountry" for="chkCC_AF">Afghanistan</label>
</li>
<li>
<input type="checkbox" class="chkCountry" id="chkCC_AL" name="chk_AL" value="AL">
<label class="lblCountry" for="chkCC_AL">Albania</label></li>
<li>
<input type="checkbox" class="chkCountry" id="chkCC_DZ" name="chk_DZ" value="DZ">
<label class="lblCountry" for="chkCC_DZ">Algeria</label></li>
<li>
<input type="checkbox" class="chkCountry" id="chkCC_AS" name="chk_AS" value="AS">
<label class="lblCountry" for="chkCC_AS">AAmerican Samoa</label></li>
</ul>
Upvotes: 1