tags:

views:

119

answers:

4

So, throughout my entire document, I would like for every single time the user hovers over an element with a specific class name, I would like a class to be added.

My CSS looks like this:

.hotspot:hover, .hotspothover
    {
    border: 4px solid #fff;
    box-shadow: 0px 0px 20px #000;
    -webkit-box-shadow: 0px 0px 20px #000;
    -moz-box-shadow: 0px 0px 20px #000;
    z-index: 1;
    }

Class name: "hotspot".

I tried this, but it doesn't seem to work:

    $("#hotspot.hover #hotspothover").addClass("bubble bottom");

Thanks.

+1  A: 

You were close!

add a comma:

$(".hotspot:hover, .hotspothover").addClass("bubble bottom");
David Murdoch
How do I override the other class. E.g. how do I replace the CSS attributes currently associated with 'hover' and replace it with the CSS attributes associated with my other class - rather than adding it.
marcamillion
You would need to define either the `bubble` or `bottom` classes in such a way that they override. Usually just having them load later will do it, but you could also add `!important` to declarations that are stubborn.
artlung
@artlung. +1 to what you said.
David Murdoch
+1  A: 

Here's how to do it.

$(".hotspot:hover, .hotspothover").addClass("bubble bottom");

helios456
+1  A: 

Something like

$('.targetClass').hover(function() { $(this).toggleClass('hovered'); });

should work if you are using jQuery 1.4.2

rmurphey
+1  A: 

Maybe I misunderstood, but I thought you wanted it added on hover. This shows that:

$('.hotspot, .hotspothover').hover(function(){
    $(this).addClass('bubble bottom');
});

If you want them also removed when hovered off, then change addClass to toggleClass.

UPDATE: Questioner followed up and asked:

Now what happens if I wanted them to replace 'hotspot' or 'hotspothover' ? As in, for all items on the page with class hotspot or the class 'hotspothover' replace it with 'bubble bottom'

I would change it like this:

$('.hotspot').hover(function(){
    $(this).addClass('bubble bottom').removeClass('hotspot');
});
$('.hotspothover').hover(function(){
    $(this).addClass('bubble bottom').removeClass('hotspothover');
});

Now, you could do this too, but if you want to be specific about the actions, doing it as two sets of calls makes more sense.

$('.hotspothover, .hotspot').hover(function(){
    $(this).addClass('bubble bottom').removeClass('hotspothover hotspot');
});
artlung
can I just use the class name 'hotspothover' rather than '.hotspot:hover'?
marcamillion
Yes. What my code is saying is this: "For all the items on my page with the class `hotspot` or the class `hotspothover` - if they get hovered over, permanently add the classes `bubble` and `bottom` to them. Also, if you want the classes added on hover, and removed on un-hover, use `toggleClass`
artlung
Now what happens if I wanted them to replace 'hotspot' or 'hotspothover' ? As in, for all items on the page with class hotspot or the class 'hotspothover' replace it with 'bubble bottom'.
marcamillion
I updated my answer.
artlung
Perfect...Thnx.
marcamillion