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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:50:06+00:00 2026-06-15T22:50:06+00:00

Example HTML (for the sake of clarity): <nav> <ul> <li class=top-navbar-channels> <a href=#></a> <div

  • 0

Example HTML (for the sake of clarity):

<nav>
    <ul>
        <li class="top-navbar-channels">
            <a href="#"></a>
            <div class="dropdown-menu">BLAH, BLAH, BLAH!</div>
        </li>

        <li class="top-navbar-about">
            <a href="#"></a>
            <div class="dropdown-menu-about">BLAH, BLAH, BLAH!</div>
        </li>

        <li class="top-navbar-search">
            <a href="#"></a>
            <div class="dropdown-menu-search">BLAH, BLAH, BLAH!</div>
        </li>
    </ul>
</nav>

Example jQuery code:

jQuery(document).ready(function($){

    $('.dropdown-menu').on('show', function () {
        $('.top-navbar-channels > a').addClass('selected');
    });
    $('.dropdown-menu').on('hide', function () {
        $('.top-navbar-channels > a').removeClass('selected');
    });

    $('.dropdown-menu-about').on('show', function () {
        $('.top-navbar-about > a').addClass('selected');
    });
    $('.dropdown-menu-about').on('hide', function () {
        $('.top-navbar-about > a').removeClass('selected');
    });

    $('.dropdown-menu-search').on('show', function () {
        $('.top-navbar-search > a').addClass('selected');
    });
    $('.dropdown-menu-search').on('hide', function () {
        $('.top-navbar-search > a').removeClass('selected');
    });

});

For those who are curious… the jQuery code adds a new class selected to the active menu item’s link. In my case it’s Twitter Bootstrap-based collapsible menu, where active means, the menu item is not collapsed i.e. open.

Now, the question is, can the jQuery code be optimized (i.e. same functionality with less code)? If so, how?

  • 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-15T22:50:07+00:00Added an answer on June 15, 2026 at 10:50 pm

    Add a common class to common main elements so you can use that single class as the selector. You can also combine the events into one on() call and use toggleClass() on the link. on() allows for multiple space separated events

    Example

    <div class="dropdown-menu menu_content">
    

    Then for jQuery:

    $('.menu_content').on('show hide', function () {
            $(this).siblings("a").toggleClass('selected'); 
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Sass (.scss) for my current project. Following example: HTML <div class=container desc>
That's my question, and here's my example code. HTML: <a class=fancybox rel=group href=img1.jpg> <img
Example HTML: <div class=elem-1></div> <div class=elem-2></div> <div class=elem-2></div> <div class=elem-3></div> <div class=elem-4></div> <div class=elem-4></div>
Given an example html string <table> <tr> <td class=td height=25>Upstream Power</td> <td class=td>25.2 dBmV</td>
Given the following HTML example... <div id='div1'>div one</div> <div id='div2'>div two</div> ...I found that
Consider the following example: HTML: <div id=cool-div></div> JS/jQuery: var ComplexObject = function() { this.Append
So this is my example HTML: <div name=John Doe></div> Note : I know all
example.html contains: <a href=mypage.html data-str=foo>Test</a> Is it possible to access the value of data-str
I have my HTML setup as follows (IDs are madeup for sake of example):
ASP.NET MVC can generate HTML elements using HTML Helpers, for example @Html.ActionLink() , @Html.BeginForm()

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.