0

I would like change <li> css class dynamically. I am using asp.net masterpage. In below example If I click on AboutUs.aspx link on page load I want change <li class="single"> to <li class="active single">.

How to do that?

 <div class="nav-collapse">
     <ul class="nav nav-pills">
           <li class="active single"><a href="Default.aspx">HOME
                 <i>company home</i>
                  </a>
            </li>
            <li class="single"><a href="AboutUs.aspx">About Us
                 <i>want to say</i>
                  </a>
            </li>
4
  • 1
    $(this).addClass('active'); ??? Commented Apr 2, 2013 at 14:40
  • 2
    Why don't you let ASP add that active class to the URL? Commented Apr 2, 2013 at 14:40
  • THis may help: stackoverflow.com/questions/5735171/… Commented Apr 2, 2013 at 14:41
  • When you load the page you have the links render with the class. You know what page is rendering the content through the Request object so it's just a matter of when you render the links to match the page to the href and change the color. What have you tried so far? Commented Apr 2, 2013 at 14:41

6 Answers 6

1

As @scrappedcola have said, you don't have to do it in Javascript as this is totally doable from server side code. But if you insist, here are the steps:

1 You have to tell Javascript which item in the menu you want highlighted. Create unique identifiers for each menu item and tell the server side code to set that. For example:

<li class="single home">
    <a href="Default.aspx">HOME<i>company home</i></a>
</li>
<li class="single aboutus">
    <a href="AboutUs.aspx">About Us<i>want to say</i></a>
</li>
<script>
    var activeLink = 'aboutus';
</script>

2 Create and call the Javascript/jQuery function to change the CSS class:

$("." + activeLink).addClass("active");
Sign up to request clarification or add additional context in comments.

Comments

1
$('.nav-collapse > ul > li > a').click( function(){
   $('li').removeClass('.active');
   $(this).parent().addClass('.active');
});

Comments

1

Like so:

$('li.single').click(function(){
    $('li').removeClass('active');
    $(this).addClass('active');
});

Here's a jsFiddle example - http://jsfiddle.net/HNKeA/1/.

1 Comment

it's going through a page load so this will work up until the page reloads
0
$('.nav-pills a').click(function(){
    $('.nav-pills li').removeClass('active');
    $(this).parent().addClass('active')
})

Comments

0
 $('li.single').click(function(){
      $(this).addClass('active');

  });

or

 $('li.single').click(function(){
         $(this).attr('class', 'active single');
      });

Comments

0
$("ul li:first").attr('class', 'newClass');

just change the selector accordingly. This should change every li whats nested in ul

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.