1

I'm using jQuery UI Menu for navigation purpose. I want that when one item is clicked it's background color should change (to show active state) and when user clicks on other item then the new item's color should change and the previous one's color should revert back to original.

I used addClass for that but somehow it's not working, let me know where I'm doing wrong.

Fiddle link

HTML:

<ul id="menu" class="nav">              
   <li><a href="#" >Item 1</a></li>
   <li><a href="#">Item 2</a></li>
   <li><a href="#">Item 3</a></li>
   <li><a href="#">Item 4</a></li>
   <li><a href="#">Item 5</a></li>
   <li><a href="#">Item 6</a></li>
</ul>

CSS:

.selected{
    color:red;
}

jQuery

  $(function() {
    $( "#menu" ).menu();
  });
  $(function () {
  $(".nav a").click(function () {
    $(this).parent().addClass('selected'). // <li>
      siblings().removeClass('selected');
  });
  });
2
  • You must use background-color instead of color. Commented May 30, 2013 at 5:07
  • Yes, got it, stupid mistake. Commented May 30, 2013 at 5:16

3 Answers 3

2

Apply style on the anchor instead of li. since anchor's inherited red color gets overridden by .ui-widget-content a

.selected a {
    color:red;
}

Demo

With background color:-

Demo

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

5 Comments

Oops, just had to use background-color instead of color.
If I do ".selected a {color:inherited;}" then it will take priority over ".ui-widget-content a" right ??
@ChankeyPathak suit yourself. This has both the options working.. :)
@SarveshKumarSingh not really, it is inherit. But then you will have to have both the style rules like jsfiddle.net/BNWBX.
@PSL yes that ".selected {color:red;}" is already there.. thanks for confirming. :)
0

Please have a look at http://jsfiddle.net/PaHXs/8/

May i know is this your requirement or not.

I did change in

.selected{
    background-color:red;
}

Comments

0
$(this).parent().siblings().removeClass('selected');
$(this).parent().addClass('selected');

.selected{
    color:red;
}

.selected a {
    color:inherit;
}

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.