6

Here is an example: https://jsfiddle.net/6kg43qfr/

Code:

Jquery:

$('#foo').css('background-color', '#f8f7f7');

Html:

<div id="foo">
test
</div>

CSS:

#foo:hover{

  background-color: red;

}

Question: Why doesn't the hover work?

3
  • 2
    It does not remove :hover. jQuery css method adds inline css. This is more important than class or ID, so :hover is ignored. Commented Apr 4, 2016 at 19:22
  • How do I get around this? Commented Apr 4, 2016 at 19:24
  • My question here is: Do you simply want to 'get around this' and write some piece of shit-ass code or you want to have a maintainable code, that can be reused and improved in future? Commented Apr 4, 2016 at 19:31

3 Answers 3

3

That is because how you set the color in your javascript code. Inline styles has more priority then styles applied to classes or id's

There are actually many rules, of how to properly override styles. Please take a quick look at this http://www.hongkiat.com/blog/css-priority-level/

I strongly suggest you to read more about css before proceeding with the project, in order to keep code clean and maintainable.

in order to fullfill your needs, take a look at this fiddle: https://jsfiddle.net/6kg43qfr/2/

$('#foo').addClass("green-background")
Sign up to request clarification or add additional context in comments.

5 Comments

How do I get around this?
@JakeStainer, I just edited my answer, this is how you CAN do this, if you need javascript.
JakeStainer, You should NOT follow the code, @MdMahfuzurRahman suggested. This is example of awfull, unmaintainable approach. Also you should NOT use !important for this case. I strongly advice you to read some 'styling' literature.
addClass() is a good solution to this issue, re-uses existing classes without overriding.
@AlexanderCapone I agree that addClass is the CORRECT solution here. Thanks Alex.
2

Because the $('#foo').css() function puts the style in a style attribute on the element, which therefore overrides the stylesheet.

4 Comments

How do I get around this?
@JakeStainer See Md Mahfuzur Rahman's answer, which works. And I don't know who's downvoting him.
@MrLister, it was me. I did explain myself in the comment above.
@AlexanderCapone My apologies. I assumed from the OP's question that the JavaScript was a necessary evil and didn't realise that the real solution could be much simpler.
2

The best solution is:

#foo:hover{  
  background-color: red;  
}

#foo {
  background-color: #f8f7f7;
}

Or

You also can use this:

$('#foo').css('background-color', '#f8f7f7').hover(
function(){
    $(this).css('background-color','red');
},
function(){
    $(this).css('background-color','#f8f7f7');
});

5 Comments

Can anyone explain the reason of downvoting ?
I can explain. Why do you suggest Jake Stainer to use javascript code to apply background-color instead of teaching him how to do it? If I would have a chance to see this piece of code in real project, I would have a serious conversation with that person.
As he is using js code to apply background-color, I also suggest him to do all changes using js. You also suggest him js. Why didn't suggest him to use css only ?
My solution can be put into some other function's handlers, and still left maintainable and easily replenishable.
yes, the pure css part, on top is the best solution, if only css here is enough.

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.