Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a vertical menu like:

XML
<nav>
    <ul>
        <li><a></a></li>
        <li><a></a></li>
        <li><a></a></li>
    </ul>
</nav>


and made 2 classes for bg-color: default , selected
I want to apply "selected" class to the newly selected item and get the older or previousely selected item "default" class

css:
.defaultBg{ background-color: white; }
.selected{ background-color: blue; }

How can i assign this classes to those list items respectively with jquery when clicking on link tag
<a></a>
within them
Posted
Updated 23-Sep-13 4:54am
v4

You may not want the tag, because the default for an a tag is to follow a link. You can prevent the default action from occuring, but why adding the element if you don't use it.
Further it's quite simple :

JavaScript
$(document).ready({ function(){
   $("li").click(function(e){
      // A LI is clicked
      // Set all other li's to not selected
      $("li").removeClass("selected");

      // Add selected class to the clicked li
      $(this).addClass("selected");
  });
});


Note the above code assumes you removed the
tags from the li elements

So to complete your question :

JavaScript
$(document).ready({ function(){
   $("li").click(function(e){
      // A LI is clicked
      // Set all selected li's to not selected and 
      $("li.selected").removeClass("selected").addClass("defaultBg");

      // Add selected class to the clicked li
      $(this).removeClass("defaultBg").addClass("selected");
  });
});


This must do the trick
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 23-Sep-13 11:05am    
Well, it should work but... makes no practical sense at all. Why? The items are not selectable anyway.
I tried to explain why and provided more reasonable advice, please see my answer.
—SA
The problem is: a list item can be any HTML element, which is not "selectable". All you can do is to detect if a mouse pointer is over the item (then you can use jQuery .hover() method) or something like this.

My best advice would be this: give up such highlighting, as it would only degrade the quality of your page, no matter how you do it. You see, good design rules dictate that you should never decorate the rendering of context with some effect if it does not serve a clearly defined purpose. If selection was the property of some HTML, highlighting would be natural. Usually, when an element is highlighted, it tells the user: "click me". But your list items are not selectable I believe you are not going to process a click. In your case, it will be only confusing and won't help to make your content more readable and accessible, which should be your main goal.

—SA
 
Share this answer
 
first

SQL
$("#rightPanel ul li.selectedBgColor")
        .toggleClass("selectedBgColor")
        .toggleClass("defaultBgColor");


then:

C#
if($(this).parent().hasClass("selectedBgColor")){

        }
        else if($(this).parent().hasClass("defaultBgColor")){
            $(this).parent().toggleClass("defaultBgColor");
            $(this).parent().toggleClass("selectedBgColor");
            var elementID = $(this).attr("value");
            var pos = $("#"+elementID).offset().top;
            $("html, body").animate({scrollTop:pos-100},500);
            return false;
        }
 
Share this answer
 
Comments
Eduard Keilholz 23-Sep-13 11:04am    
Why did you choose to post this code as an answer? The code does a lot of stuff but not what the poster asked for, and in a dramatically inefficient way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900