3

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?

5
  • 1
    probably you have another class which is over writing them, a link example would be great Commented May 26, 2011 at 13:59
  • 2
    and why you want to do this sort of thing with cookies is beyone me :P Commented May 26, 2011 at 14:00
  • Are any errors occuring? Commented May 26, 2011 at 14:01
  • Tomgrohl, no errors to report Commented May 26, 2011 at 14:06
  • its difficult to suggest a usefull idea, unless you can provide information about your website or cms and coding language Commented May 26, 2011 at 14:07

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

bangs head on desk -- so simple but exactly what it was... I had a specific selector for the anchor tag with a different height defined. Thanks!
2

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

1 Comment

Thanks Vilmantas, that link points out what I need to understand
1

Use something like the Web Developer toolbar or DOM Inspector in Firefox to work out what's actually affecting your styles.

Comments

1

It sound like you want to use a class on a specific link when your on that page, so you could have a class on the body, like so:

<body class="home">

And in your CSS:

.home a.homeLink:link{
...
}

Wouldn't need JS at all then.

Done an example here:

http://jsfiddle.net/NbVVr/

Not sure if this is what you want?

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.