I've created a VF page, in which I'm displaying a dynamic Checkbox. Now i want when i check the checkbox, the background color should be change and when uncheck the checkbox, background color should be normal as original.

How to apply Css to dynamic checkbox. How to get ID in this condition.

My apex page code is

<td style="display: inline-table">
            <div id="Territory" style="">Territory: <br/>

                <apex:selectcheckboxes layout="pagedirection" value="{!Territory}" onchange="PutValue(this.id,this.value);" style="">                    
                   <apex:selectoptions value="{!TerritoryValues}" />  



Please suggest.

2 Answers 2


A checkbox does not have background color so setting the background color property will not work.<apex:selectcheckboxes generates a html table and put every checkbox inside a <td> element. One possible solution is to get the parent <td> and apply the background color to it when user click on it. Based on the state whether it is checked or not, the background color property of the <td> element can be changed as below.

<apex:page controller="sampleCon">
    <script type="text/javascript">
        function changeColor(elem){
            var pElem = elem.parentElement;
                pElem.style.backgroundColor = "red";
                pElem.style.backgroundColor = "";
        <div id="Territory">Territory: <br/>
            <apex:selectCheckboxes value="{!countries}" layout="pagedirection" onclick="changeColor(this);">
                <apex:selectOptions value="{!items}"/>


enter image description here

  • @Amol If this answer has solved your question you can use the check mark next to it to mark it as such. That will help others who are looking at it to locate the accepted solution.
    – Saroj Bera
    Commented Dec 2, 2016 at 10:18

Would suggest you to try this:

#Territory input[type=checkbox]:checked {
   background-color: red !important;

By this css you're selecting your div by Id and then apply background-color: red to every input that have type checkbox and in :checked state.

It's hard to provide you 100% correct solution since there're always salesforce styles in play and they could override your css. In order to make your css work, you need to play with developer console in chrome(or simular instruments in other browsers).

Edited: As mentioned by Saroj Bera checkboxes don't have a background color property. You need to apply a background color to parent element.

  • 1
    A checkbox does not have background color. You can check this by adding background-color property here in this link w3schools.com/cssref/tryit.asp?filename=trycss3_checked. Did you applied this before anywhere?
    – Saroj Bera
    Commented Dec 2, 2016 at 8:49
  • @SarojBera No i've not used before. This is possible for my condition or Not?
    – Amol
    Commented Dec 2, 2016 at 9:03
  • @EvAzi I've tried as you mention in answer but its not working
    – Amol
    Commented Dec 2, 2016 at 9:04
  • @Amol Check my answer, I hope this will help you.
    – Saroj Bera
    Commented Dec 2, 2016 at 9:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .