0

I am wondering how I would be able to modify an existing style that is applied to the hover state of an element via jQuery.

For example:

.button { color:red; }
.button:hover { color:blue; }

I would like to apply the style font-size:14px; to .button:hover. The value of font-size will vary depending on which other elements on the page the user has interacted with previously.

2
  • 1
    modify...where? Adding it to the .button:hover css is out of the question I assume? Commented Dec 11, 2012 at 18:09
  • 2
    Why using jquery for this? use with css instead. Commented Dec 11, 2012 at 18:12

4 Answers 4

4

You can't modify the :hover styles (or any other psuedo-class) using JavaScript, because technically they aren't part of the DOM.

What you'll need to do is add those styles to a different class in your CSS, then add/remove that class using JavaScript/jQuery.

CSS:

.button.bigger { font-size:14px; }

jQuery:

$('.button').on('hover', function(e) {
    $(this).toggleClass('bigger');
});
Sign up to request clarification or add additional context in comments.

3 Comments

As always, keep in mind that your hover styles won't work on touchscreen devices.
Thats OK. I'm just building for desktop :)
Oh I was too slow. +1 for being much quicker! :)
2

what about this with css:

.button:hover { color:blue; font-size:14px;}

and with jQuery:

 $('.button').hover(function(){
    $(this).css({"font-size":"14px"}); // increases on mouseover
 },function(){
    $(this).css({"font-size":"12px"}); // decreases on mouseout
 });

Comments

1

You would need to either make an event handler in jQuery or make a class specific hover in CSS.

Here is a working example of both:

http://jsfiddle.net/dboots/GHzSs/

JS:

$('#large').hover(function(e) {
        var t = $(this);
        t.css('fontSize', '14px');
}, function(e) {
        var t = $(this);
        t.css('fontSize', '11px');
});​

HTML:

<div class="button">Regular Button</div>
<div class="button large">Large Button (by class/css)</div>
<div class="button" id="large">Large Button (by id/jquery)</div>​

CSS:

.button {
    padding: 5px;
    border: 1px solid #999999;
    width: 250px;
    margin: 5px 0;
}

.button:hover {
    border: 1px solid #000000;
}

.large:hover {
    font-size: 14px;
}

Comments

1

add some class or id to your button..target it using jquery and add css to it.

e.g.

$('.buttonToBeChanged').on('hover',function(){
    $(this).css('font-size',14);
});

That should do. See this for reference. http://api.jquery.com/css/

EDIT : The code will set the font to 14px after that. even after hover out. To give it a toggle effect, use the answer by @Blazemonger. That is just fine. Or you can use mouseenter and mouseout events

EDIT AGAIN - CSS too will do the job alone as @Jai mentioned.

.button:hover { color:blue; font-size:14px;}

2 Comments

hover may take 2 functions as arguments. In that case it is a shorthand for mouseenter and mouseleave. If you pass solely one function, it will attach the same function to both mouseenter and mouseleave.
Thats right! absolutely. I just showed a way. Now the answer has been edited to answer all the queries.

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.