D.T
D.T

Reputation: 537

change background image of a div when checkbox is checked

I hide the default checkbox and use a div with custom checkbox image instead:

<aui:form>
<c:if
    test="<%=company.isAutoLogin() && !PropsValues.SESSION_DISABLED%>">
    <div class="rememberImage ftr" id="rememberImg">
        <aui:input checked="<%=rememberMe%>" name="rememberMe" id="rememberMe"
                            type="checkbox" cssClass="remember"/>
    </div>
</c:if>
</form>

//omiited

<aui:script use="aui-base">     
        function changeBG() {
            if (this.checked) {
                document.getElementById('rememberImg').style.backgroundImage = 'url(../img/chk_none.png)';
            } else {
                document.getElementById('rememberImg').style.backgroundImage = 'url(../img/chk_check.png)';
            }       
        }

        document.getElementById('_58_rememberMe').addEventListener('change', changeBG);


        var password = A.one('#<portlet:namespace />password');

        if (password) {
            password.on(
                'keypress',
                function(event) {
                    Liferay.Util.showCapsLock(event, '<portlet:namespace />passwordCapsLockSpan');
                }
            );
        }
    </aui:script>

This does not work at all. Any suggestions?? Much appreciated!

UPDATE: add more lines of code that I think maybe have problems

Upvotes: 0

Views: 2792

Answers (5)

M.Melih
M.Melih

Reputation: 36

function SetBackground(elm){
  if($(elm).is(":checked"))
    {
  $("#rememberImage").css({"background-color":"gray"});
      }
  else
    {
    $("#rememberImage").css({"background-color":"white"});
    }
  
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="rememberImage ftr" id="rememberImage">              
    <input checked="<%=rememberMe%>" name="rememberMe" id="rememberMe" type="checkbox" onchange="SetBackground(this)"/>                        
</div>

I don't know how your custom checkbox works but, you can add onchange event and check if checkbox is checked then change the background of div.

Best regards

Upvotes: 0

markoffden
markoffden

Reputation: 1468

I saw a proper answer already above, but to avoid intruding HTML tags with JS listeners, what considered as not the best practice, I will offer you this solution...

function changeBG() {
    if (this.checked) {
        document.getElementById('myElement').style.backgroundImage = 'url(.....)';
    } else {
        document.getElementById('myElement').style.backgroundImage = 'url(.....)';
    }       
}

document.getElementById('myInput').addEventListener('change', changeBG);

Hope it will help you :)

Upvotes: 3

VIJAY P
VIJAY P

Reputation: 1433

div{
    margin: 20px 0;
}
input[type=checkbox] {
    display: none
}
input[type=checkbox]+label {
    background: url(http://s17.postimg.org/phsoii5vf/check.png) no-repeat;
    padding-left: 30px;
    padding-bottom: 5px;
    padding-top: 2.5px;
    height: 18px;
}

input[type=checkbox]:checked+label {
    background: url(http://s2.postimg.org/zbjg138np/check_tick.jpg) no-repeat;    
}
<div>
    <input type="checkbox" id="chk1">
    <label for="chk1">Custom Checkbox1</label>
</div>
<div>
    <input type="checkbox" id="chk2">
    <label for="chk2">Custom Checkbox2</label>
</div>
<div>
    <input type="checkbox" id="chk3">
    <label for="chk3">Custom Checkbox3</label>
</div>
<div>
    <input type="checkbox" id="chk4">
    <label for="chk4">Custom Checkbox4</label>
</div>

not required javascript.you can do it from css.

<div>
<input type="checkbox" id="chk">
<label for="chk">Custom Checkbox1</label>
</div>

input[type=checkbox] {
    display: none
}
input[type=checkbox]+label {
    background: url(../images/check.png) no-repeat;
    padding-left: 30px;
    padding-bottom: 5px;
    padding-top: 2.5px;
    height: 18px;
}

input[type=checkbox]:checked+label {
    background: url(../images/check_tick.png) no-repeat;    
}

Upvotes: 1

weidler
weidler

Reputation: 676

You can do it like this: Add an onclick attribute to the checkbox that triggers a toggle function. As I cant test it with your code (missing the rest) I can only provide you an enxample where the body background gets changed

<input id="check" type="checkbox" onclick="toggle();"> Click me

<script>

    function toggle() {

        if( document.getElementById("check").checked ) {
                document.getElementsByTagName("body")[0].style.backgroundColor="red";
        }
    }

</script>

Upvotes: 1

Ivin Raj
Ivin Raj

Reputation: 3429

you can try this one:

document.getElementById('rememberImage').style.background = 'url(../img/chk_check.png)';

Upvotes: 0

Related Questions