2

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.

4 Answers 4

2

You were close!

add a comma:

$(".hotspot:hover, .hotspothover").addClass("bubble bottom");
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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. +1 to what you said.
1

Here's how to do it.

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

Comments

1

Something like

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

should work if you are using jQuery 1.4.2

Comments

1

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');
});

3 Comments

can I just use the class name 'hotspothover' rather than '.hotspot:hover'?
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
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'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.