Click here to Skip to main content
15,894,313 members
Please Sign up or sign in to vote.
1.36/5 (4 votes)
See more:
I have

HTML
<li class="checkhere active" id="aaaa"> <a> aaaaa </a>   </li>
<li class="checkhere" id="bbbb"> <a> bbbbb </a>   </li>
<li class="checkhere" id="cccc"> <a> ccccc </a>   </li>
<li class="checkhere" id="dddd"> <a> ddddd </a>   </li>


in jquery i want

if bbbbb clicked , then remove active class from all , then show class = active for only bbbbb

same if ccccc , clicked, the remove active class from all ,then show class=active only ccccc



show code for html and jquery
Posted

JavaScript
$(document).ready(function(){
    $('li').on('click', function(){
        //alert('clicked');
        $(this).siblings().removeClass('active');
        $(this).addClass('active');
    });
});


HTML
<ul>
    <li class="checkhere active" id="aaaa"> <a> aaaaa </a>   </li>
    <li class="checkhere" id="bbbb"> <a> bbbbb </a>   </li>
    <li class="checkhere" id="cccc"> <a> ccccc </a>   </li>
    <li class="checkhere" id="dddd"> <a> ddddd </a>   </li>
</ul>


CSS
li{
    cursor : pointer;
}
.active{
    color : red;
}


Here is working solution
 
Share this answer
 
v3
Comments
maulikshah1990 17-Jan-14 7:14am    
can i use $('.checkhere').click and not only li
Hammad 17-Jan-14 7:17am    
Yup You can use the class also. you can check on fiddle It was updated and now work with class
maulikshah1990 17-Jan-14 7:21am    
but when i use , that ,

for exp.

test1
test2

and i use jquery

$(document).ready(function(){
$('.menulist').on('click', function(){
//alert('clicked');
$(this).siblings().removeClass('active');
$(this).addClass('active');
});
});


when i click test2.aspx, page loads and i goto test2.aspx (it should make test.aspx li active) , but test1.aspx is active
i use master page and content page
Hammad 17-Jan-14 7:25am    
Place your code in test2.aspx page and instead of using 'this' use '#Id'
e.g.

$(function(){
$('#test2').siblings().removeClass('active');
$('#test2').addClass('active');
});
Member 14531428 24-Jul-19 19:26pm    
454
use this::

PHP
$(function(){
    $(".checkhere").bind("click",function(){

   $.each( $(".checkhere"), function( i, l ){
  $(this).removeClass("active");
});

          $(this).addClass("active");
    });


});
 
Share this answer
 
C#
$('.menulist').each(function() {
        if (window.location.href.indexOf($(this).find('a:first').attr('href')) > -1) {
            $(this).addClass('active').siblings().removeClass('active');
        }
    });
 
Share this answer
 

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