I'm trying to change the background position of an element using a checkbox, but is not working.
Here is the code:
x = false;
function Check() {
if (x) {
document.getElementById("checkboz").style.backgroundPosition = '0 0';
document.getElementById("checkboz").style.backgroundPosition = '-28px 0';
x = false;
} else {
document.getElementById("checkboz").style.backgroundPosition = 'none';
document.getElementById("checkboz").style.backgroundPosition = '0 0';
x = true;
}
}
#checkboz {
background: url(http://movimientomore.com/images/2016/12/21/sprite-check-01.png);
background-size: 55px;
background-repeat: no-repeat;
width: 28px;
height: 28px;
}
<div id="checkboz" class="checkbox">
<label for="modlgn-remember">
<input id="modlgn-remember" name="remember" class="inputbox" value="yes" type="checkbox">Recordarme</label>
</div>
Thank you so much for your help :)
I have updated your fiddle to show a working version of the functionality you are seeking.
Your fiddle
Like others have said in the comments, the Check function was never being called, but additionally, it is better to swap classes out on the elements instead of trying to manually manipulate the styles.
With the following css classes:
without the !important on the css classes the existing background style will take precedence over the new classes.
I hope that help!