Jigar Jain
Jigar Jain

Reputation: 1447

Navigation applying class using jquery

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 ?

Upvotes: 1

Views: 187

Answers (1)

Paul Fleming
Paul Fleming

Reputation: 24526

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/

Upvotes: 3

Related Questions