Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding css properties to jquery element has different result - class vs. css() method

Tags:

jquery

css

For my site, I'm saving the navigation menu's selected name in a cookie and after postback I read the cookie and then apply a background image to that selected menu item (using the same image I use for hovering).

I made a class for my "selected" menu items:

.selected
 {
color: Green;
height: 40px;
background: url(images/menu_hover.jpg) bottom no-repeat; 
 }

When I check for a cookie after postback I want to apply this class:

$("#" + $.cookie(cookieName)).addClass("selected");

It seems it only applies the background image, not the color or the height. In order to have the color and height work at all, I have to explicitly set those using the .css() method:

$("#" + $.cookie(cookieName)).css({ 'color': "green" });
$("#" + $.cookie(cookieName)).css({ 'height': "40px" });

Just curious if anyone has an idea as to why this occuring?

like image 629
proggrock Avatar asked Dec 09 '25 20:12

proggrock


2 Answers

It sounds to me like a css specificity issue -- you may have color and height defined elsewhere with a more specific selector.

like image 67
newtron Avatar answered Dec 11 '25 09:12

newtron


May it be that other CSS selectors match your element as well and they have higher priority?

You may find more info here: CSS: Understanding the selector's priority / specificity

like image 45
Vilmantas Baranauskas Avatar answered Dec 11 '25 09:12

Vilmantas Baranauskas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!