Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9180887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:09:35+00:00 2026-06-17T18:09:35+00:00

I want to add a class to the selected ‘li’ and at the same

  • 0

I want to add a class to the selected ‘li’ and at the same time, remove the class:selected from previous selected li element.

I have worked on it hours and still haven’t got any luck. I also checked others questions, but their solutions don’t work for me.

Help please….

<ul id='mainView' class='menu' style='float: left; clear: both;'>      
    <a href="/Link1"><li>Patient</li></a>
    <a href="/Link2"><li>Recommendations</li></a>

</ul>

<script type="text/javascript">
    $(document).ready(function () {
        $('.menu ul a').on('click', function (event) {
            $('.menu ul a.selected').className = '';
            alert($(this).attr('id'));
            $(this).attr('class') = 'selected';
        });
    });

//    $('.menu li').on('click', function () {
//        $('.menu li.selected').className = '';
//        this.className = 'selected';
//    });
</script>

Update:

I did put a inside li, but if I click on the li not the a inside of the li, the webpage does not redirect. That’s the reason why I do it in a reversed way.

Update 2
The reason why the selected li does not get the “selected” class is because the whole webpage is redirected to a new page which has the same navigation bar.
So now the question is how to highlight the selected li(it was selected on the previous page) on the new webpage.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:09:36+00:00Added an answer on June 17, 2026 at 6:09 pm

    Inside an UL everybody (even a browser) is expecting to see a LI

    so your HTML:

    <ul>
        <li><a href="/Link1">Patient</a></li>
        <li><a href="/Link2">Recommendations</a></li>   
    </ul>
    

    And your jQ:

    $('ul li').click(function(){
      $(this).addClass('selected').siblings().removeClass('selected');
    });
    

    Building web pages you should know how to treat LI elements. Simple, like dummy containers with minimal styling.
    That means that you rather add a display:block … float:left and other cool stuff to the <A> elements, than setting a padding there you go with your full-sized-clickable A elements.


    Additionally (if you don’t have time to play with CSS) to make a LI fully clickable use:

    $('ul li').click(function(){
      var goTo = $(this).find('a').attr('href');
      window.location = goTo ;
      // $(this).addClass('selected').siblings().removeClass('selected'); // than you don't need this :D
    });
    

    After the OP late edit – and to answer the question

    After the pages refreshes to get which one is the active one use:

    // ABSOLUTE PATH
    var currentPage = window.location;
    
    // RELATIVE PATH
    //  var currentPage = window.location.pathname;
    
    $('li a[href="'+ currentPage +'"]').addClass('selected');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 list and i want to add a class =last to the
I have a class which I want to add a property with using formula
So I want to be able to add/remove class methods at runtime. Before you
I have div-elements when i click on DIV, i add class selected of this
I want to be able to just add a class called selected to the
When the page has loaded, I want to add a class to an element
I have the following code which tries to add a class of selected to
I basically have this markup coming from my JSP. I add class in each
I want to add class atttributes to a superclass dynamically. Furthermore, I want to
I want to add a class to certain input or label elements in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.