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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:55:26+00:00 2026-06-03T13:55:26+00:00

I have this menu in _Layout.cshtml : <td class=MenuStructure> <ul id=menu> <li>@Html.ActionLink(First Page, Page1Action,

  • 0

I have this menu in _Layout.cshtml :

<td class="MenuStructure">
    <ul id="menu">
        <li>@Html.ActionLink("First Page", "Page1Action", "Main")</li>
        <li>@Html.ActionLink("Second Page", "Page2Action", "Main")</li>
        <li>@Html.ActionLink("Third Page", "Page3Action", "Second")</li>
    </ul>
</td>

When one of the action links is clicked, I want to set the class of the <li> which contains it to “selected” and the class of the other <li> elements to “”.

This works :

    <script type="text/javascript">
        $(document).ready(function () {
        var selMenu = '@ViewBag.SelectedMenu';
            if (selMenu == "page1") {
                $("#page1").attr('class', 'selected');
                $("#page2").attr('class', '');
                $("#page3").attr('class', '');
            }
            if (selMenu == "page2") {
                $("#page1").attr('class', '');
                $("#page2").attr('class', 'selected');
                $("#page3").attr('class', '');
            }
        });
    </script>

but it is awfully ugly. Can someone show me a more elegant way to do this?

  • 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-03T13:55:27+00:00Added an answer on June 3, 2026 at 1:55 pm

    If you add a click event handler to the group of a elements, you can easily add the class to the clicked element’s li and remove it for all siblings, regardless of how many there are. This removes the need for the if statements and the attr update for each li.

    Here’s a sample:

    http://jsfiddle.net/JjBgm/4/

    markup:

    <ul id="menu">
        <li><a href="#">one</a></li>
        <li><a href="#">two</a></li>
        <li><a href="#">three</a></li>
    </ul>
    

    jQuery:

    $(document).ready(function() {
    
        $('#menu li a').click(function() {
            $(this).parent().addClass('selected').siblings().removeClass('selected');
        });
    
    });
    

    You’d have to obviously modify this approach to your needs with MVC, but the concept should work.

    EDIT: Because you mentioned there is a round-trip to the server involved the above may not work well. In that case you could construct the clientside id based on the selected menu and control the class from there.

    $(document).ready(function () {
        var selMenu = '@ViewBag.SelectedMenu';
        $("#" + selMenu).addClass('selected').siblings().removeClass('selected');
    });
    

    This is assuming #page1, #page2, etc. refer to the <li> elements without seeing your resulting markup after server processing.

    If #page1 refers to the <a> tag, then your statement would be:

    $("#" + selMenu).parent().addClass('selected').siblings().removeClass('selected');
    

    Untested of course. Main point is dynamically construct your selector, then use sibling and parent selectors as needed to clear the class. It’s much cleaner.

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

Sidebar

Related Questions

i have this html menu: <ul id='main'> <li class='active current' id='lighty'> <a href='/'>Lighty</a> <ul>
Assuming I have this HTML structure: <ul class='menu'> <li> <div></div> <div> <div></div> <div> <a
I have this html structure <div class=top_menu> <ul> <li class=left><a href= class=family-filter>asdas</a></li> <li class=welcome>Welcome
I have a main activity, that holds the main menu. This menu has an
Currently I have a page with navigation menu. This navigation menu get it items
I have this menu structure that is used to navigate through content slider panels.
I have a simple dropdown menu and I'd like to have this menu close/hide
I have this typoscript language menu: tmpl.NavLang = HMENU tmpl.NavLang { special=language special.value =
I have this need to place my app in the share/send context menu so
I have a SELECT LIST MENU containing products and on this menu i use

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.