I can not make CSS-property 'background' work when using it in an 'if.. else' - statement. I tried all possible combinations of 'background-color' and backgroundColor' in both CSS and JQuery, but nothing is working.
The funny thing is that when exchanging the 'if..' with another property fx ('width') == '500px' everything is working fine.
Are there any special thing i should know when using 'background'? Heres the code that doesn't work - the div turns yellow when clicking even thought it should become pink..
.box {
width: 500px;
height: 200px;
background: red;
}
<script src='http://code.jquery.com/jquery-latest.min.js'></script>
<script>
$(document).ready(function() {
$('.box').click(function(){
if ($('.box').css('background') == 'red') {
$('.box').css('background', 'pink');
}
else {
$('.box').css('background', 'yellow');
}
});
});
</script>
ANSWER - this is the original answer from showdev, which i think reflects my question better - could be a help to future visitors:
$('.box').click(function() {
if ($(this).css('background-color') == 'rgb(255, 0, 0)') {
$(this).css('background-color', 'pink');
} else {
$(this).css('background-color', 'yellow');
}
});