-1

The big box here - http://jsfiddle.net/uwL3q7un/ - turns orange when you roll over it. If you click the small box it sets a flag that I want to somehow change the hover behavior of the big box to turn black when you roll over it. I tried adding a class to box:hover, as you can see, but that doesn't seem to work. Does anyone see a way to do this?

Thanks.

$(function(){
    var flag=false;
    $('#flag').click( function() {
        if(!flag) {
            $('#flag').css('background-color', 'red');
            $('#box:hover').addClass('hoverBlack');
            flag = true;
        }
        else {
            $('#flag').css('background-color', 'white');
            $('#box:hover').removeClass('hoverBlack');
            flag=false;
        }
    })
});
Steve
  • 4,534
  • 9
  • 52
  • 110

2 Answers2

1

Fiddle: http://jsfiddle.net/uwL3q7un/1/

You cannot add or remove a class on the :hover pseudo-element. You have to do it on the actual element and handle the hover state in the CSS.

JS:

$('#box').addClass('hoverBlack');
$('#box').removeClass('hoverBlack');

CSS:

#box.hoverBlack:hover {
    background-color:black;
}
Uptown Apps
  • 707
  • 1
  • 5
  • 11
1

Change this in your code http://jsfiddle.net/uwL3q7un/2/

$(function(){
    var flag=false;
    $('#flag').click( function() {
        if(!flag) {
            $('#flag').css('background-color', 'red');
            $('#box').addClass('hoverBlack');
            flag = true;
        }
        else {
            $('#flag').css('background-color', 'white');
            $('#box').removeClass('hoverBlack');
            flag=false;
        }
    })
});

And the css

#box.hoverBlack:hover {
    background-color:black;
}
Bojan Petkovski
  • 6,835
  • 1
  • 25
  • 34