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 6219483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:47:31+00:00 2026-05-24T07:47:31+00:00

when the user clicks on a menu tab i want it to remain selected

  • 0

when the user clicks on a menu tab i want it to remain selected with it a white button.

here is my attempt but its not working. if you click the home button it doesnt remain white.

html

<ul id="navigation">
    <li><a href="#"><span>HOME</span></a></li>
    <li><a href="/en-us/about.aspx"><span>ABOUT</span></a></li>
    <li><a href="/en-us/contact.aspx"><span>CONTACT</span></a></li>
</ul>

css:

body{
    background-color:orange;
}

#navigation a
{
    background: url("http://i52.tinypic.com/spxfdw.png") no-repeat scroll 0 0 transparent;
    color: #000000;
    height: 43px;
    list-style: none outside none;
    padding-left: 10px;
    text-align: center;
    text-decoration: none;
    width: 116px;
}

#navigation a span
{
    padding-right: 10px;
    padding-top: 15px;
}

#navigation a, #navigation a span
{
    display: block;
    float: left;

}

/* Hide from IE5-Mac \*/
#navigation a, #navigation a span
{
    float: none
}
/* End hide */

#navigation a:hover
{

    background: url('http://i51.tinypic.com/2iih9c9.png') no-repeat scroll 0 0 transparent;
    color: #000000;
    height: 43px;
    list-style: none outside none;
    padding-left: 10px;
    text-decoration: none;
    width: 116px;
    text-align:center
}

#navigation a:hover span
{
    background: url(right-tab-hover.gif) right top no-repeat;
    padding-right: 10px
}

#navigation ul
{
    list-style: none;
    padding: 0;
    margin: 0
}

#navigation li
{
    float: left;
    list-style: none outside none;
    margin: 0;
} 

JS

$('#navigation li').click(function() {
    $('#navigation li').addClass('selected');
});

any ideas?

  • 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-05-24T07:47:31+00:00Added an answer on May 24, 2026 at 7:47 am

    I see several modifications here.

    Firstly, when you apply selected class, you are applying to all li items, which is not true. you only want to apply the class to clicked list item.

    secondly, when you click another list item, you want to remove the selected class.

    so the code modified would be:

    $('#navigation li').click(function() {
        $('#navigation li').removeClass('selected');
        $(this).addClass('selected');
    });
    

    Most importantly, you do not have a selected class. I put a temporary selected class definition like so:

    .selected{
    border: 1px solid #888;
    background-color: #white;
    

    }

    You can see a working example here: on JsFiddle

    additionally, your a element has a gray background. so you might want to apply selected white background class to your a element also.. something like:

    $('a', this).addClass('selected'); //apply to  anchor element also
    

    and when you remove the class, follow the same deal.

    So you want to persist the button states across different Pages. Javascript is only valid as long as the page is open. As soon as the user goes to the new page, all javascript will be reset. To overcome this you can do one of these two things:

    1) If you are using a master page for menu, add runat="server" attribute to your list items, and from code behind, add selected class to appropriate list item from your child page (may be you could have a public function in your Master page)

        //Master page
        public SetHomeMenu()
        {
            liHome.Attributes.Add("class","selected");
        }
    
        //in Child page
        Master.SetHomeMenu(); 
    

    2) If you want to do it in javascript, you need to read your url, parse it, then add selected class to appropriate li

    //put this javascript in your head section or even at the bottom, just before closing
    // body tag </body>
    
        $(document).ready(function () {
            if(window.location.href.indexOf("home"))
            {
                 $("#navigation li#home").addClass("selected");
            }
            else if(window.location.href.indexOf("about"))
            {
                 $("#navigation li#about").addClass("selected");
            }
            else if(window.location.href.indexOf("contact"))
            {
                 $("#navigation li#contact").addClass("selected");
            } 
    
        });
    

    But for this to work, you need to add id attributes to your list items like so:

    <ul id="navigation">
        <li id="home"><a href="#"><span>HOME</span></a></li>
        <li id="about"><a href="/en-us/about.aspx"><span>ABOUT</span></a></li>
        <li id="contact"><a href="/en-us/contact.aspx"><span>CONTACT</span></a></li>
    </ul>
    

    For use the last solution you need to change the if statement to this example:

    if(window.location.href.indexOf(“home”) > -1)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Details: Only disable after user clicks the submit button, but before the posting back
When a user clicks a submit button I want the form to be submitted.
When the user clicks a button, I want his browser to automatically scroll to
I want to know when the user clicks the menu item he should be
I have a menu(that comes when the user clicks default menu button) in which
As soon as the user clicks the delete button my jQuery script asks the
When a user clicks on a button or link, I use the SimpleModal jQuery
I have a program which creates DialogBox window when user clicks the menu item
Im trying to create a menu item so when a user clicks on the
I have a link for menu profile. It has a class called 'user-button'. When

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.