1

I am having problems applying CSS using jQuery on navigation panel. When user clicks a link, the class .selected should get applied.

My html code is

 <ul id="example-two">
        <li><a href="#">About Us</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">People</a></li>
        <li><a href="#">Career</a></li>
        <li><a href="#">Contact Us</a></li>
        <li><a href="#">Videos</a></li>
    </ul>    

My CSS code is

#example-two li a { 
  position: relative; 
  z-index: 200;
  background-image: -moz-linear-gradient(#6C6C6C, #4A4A4A);
  border-radius:10px; 
  color: #FFF; 
  font-size: 14px; 
  display: block; 
  float: left; 
  padding: 6px 10px 4px 10px;
  text-decoration: none;
  text-transform: uppercase; 
}
#example-two li a:hover {
    background-image: -moz-linear-gradient(#ed2024, #c8171a);
    color: white; 
}

.selected{
    background-image: -moz-linear-gradient(#ed2024, #c8171a);
    color: white;
}

And my jQuery code is

<script>
$(document).ready(function() {
    $('ul#example-two li a').click(function() {
       $('ul#example-two li a').removeClass("selected");
       $(this).addClass("selected");
    });
})
</script>

Any idea, where am going wrong ?

1
  • There shouldn't be anything wrong with this. It must be an issue somewhere else. (Is this code actually being evaluated?) Commented Jul 21, 2012 at 9:07

1 Answer 1

3

You are overriding the background image value in your top CSS selector. See here for more info on css precedence.

Change .selected to #example-two li a.selected

http://jsfiddle.net/EuuhZ/1/

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

5 Comments

but there has to be something wrong somewhere..am unable to find it out
See edits. Note that your code will only work in FireFox. Is that intended?
no..i intend to make it work on all browsers.. where should i make the changes ?
@Jigar. The -moz specific background gradient will only work in FF. There are a bunch of sites that will generate the css for you. Like this one for instance: gradients.glrzad.com
yes..i had figured it out..thanks.and code is working fine after making the changes u suggested in ur edits :)

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.