1

I have a :hover style definition declared in the CSS stylesheet file:

.myclass:hover {
    border-color: red;
    background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/Om.svg/220px-Om.svg.png');
}

Now, under given conditions, I want to change the background-image for this hover style.

I don't know how to do this using JavaScript / jQuery. Is this possible? How?

9
  • I think I didn't write the question well. I know what people has answered by now (thanks), but it is not what I asked. Commented Apr 30, 2013 at 23:11
  • You mean you want to change the css style itself? Commented Apr 30, 2013 at 23:12
  • Look: the first part of the answer is a "YES" or "NO". Can I change the style? The ":hover" style, specifically. Commented Apr 30, 2013 at 23:13
  • You will have to use javascript to query the stylesheet and change the Css class. Commented Apr 30, 2013 at 23:13
  • 1
    Yes that would make more sense. But why can't you define another class with that background image and change the class based on your condition. Commented Apr 30, 2013 at 23:14

4 Answers 4

2

You can add a new style tag cascade over the previous declaration. assuming the css in in the head tag

$('head').append('<style>#element:hover {/
    background-image: url("http://new.image/url");/
}/
<style>');
Sign up to request clarification or add additional context in comments.

Comments

1
$('#element').hover(function() {
//on hover
    if(condition === true) {
        $(this).addClass('newBGImage');
    }
}, function() {
//when hover ends
    if($(this).hasClass('newBGImage')) {
        $(this).removeClass('newBGImage');
    }
});

Comments

1

Make your CSS be something like this:

.myclass:hover {
    border-color: red;
    background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/Om.svg/220px-Om.svg.png'); 
}

.specialCondition:hover {
    background-image: url('http://anotherURL');
}

And then, for that special condition do:

$('.myclass').addClass('specialCondition');

And when the special condition is no longer there, remove the class:

$('.myclass').removeClass('specialCondition');

This way you keep your background-urls where they belong, in the CSS

2 Comments

+1 I think this is what I will actually do. In fact, this is really the right thing to do! And your answer, although simple, is very well written. Thank you! I would like to accept your answer too, but Musa's dirty trick actually DOES what I wanted and asked for.
Also, surprisingly, from the answers by now which mentioned the "condition", yours is the only one which considered it correctly.
0

You want to add a class that has the new background and then on hover use something like

$(this).addClass("YourClassName");

and then on leave

$(this).removeClass("YourClassName");

Comments

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.