Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
Bold submenu items in sidebar menu

I followed the explanation below to create what I needed, but there is an additional requirement i am trying to fulfill.

highlight the navigation menu for the current page

My submenu items are anchor links on the same page as the main menu item. Example:

$(function() {
        var url = window.location.href;
        $(".imagingmenu a").each(function() {
            if (url == (this.href)) {
                $(this).closest("li").addClass("active");
               $(this).closest("li").parent().parent().addClass("active");
            }
        });
    });
.imagingmenu ul li.active a, .imagingmenu ul li a:hover {
font-weight:bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="imagingmenu">

<ul>
<li><a href="http://exampleurl-1.com">Menu item 1</a>

<li><a href="#">Menu item 2</a>

<ul>
<li><a href="#/#submenuitem1">Submenu Item 1</a></li>
<li><a href="#/#submenuitem2">Submenu Item 2</a></li>
<li><a href="#/#submenuitem3">Submenu Item 3</a></li>
</ul>

</li>
</ul>

</div>

You can see the bolding works when you hover over the submenu items, but when they are clicked and active, I want just that submenu item to be bolded and the other not to be. However, when any of the submenu items are active, all of them in the list are bolded. How can I achieve what I'm looking for?

Thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This is your problem:
$(this).closest("li").parent().parent().addClass("active");

When you click on a "Submenu Item" such as "Submenu Item 2" your code adds the active class to that item, but it also adds the active class to the parent-of-the-parent of the <li> that was clicked.

The parent of the <li> is the enclosing <ul> and the parent of that is the
<li><a href="#">Menu item 2</a> so what you end up with is this (notice the places where the active class was added)

<ul>
    <li><a href="http://exampleurl-1.com">Menu item 1</a>

    <li class="active"><a href="#">Menu item 2</a>

    <ul>
        <li><a href="#/#submenuitem1">Submenu Item 1</a></li>
        <li class="active"><a href="#/#submenuitem2">Submenu Item 2</a></li>
        <li><a href="#/#submenuitem3">Submenu Item 3</a></li>
    </ul>

    </li>
</ul>

The <li> for "Menu item 2" gets the active class so it is bold according to the CSS rule
.imagingmenu ul li.active a

That is, the whole <li> including all its children — the inner <ul> and all its <li>s are bold.

Try commenting out that line as below (I also added some console.log()s so I could see what it was doing)

$(function() {
    var url = window.location.href;
    $(".imagingmenu a").each(function() {
        console.log(`this.href=${this.href} compare to url ${url}`);
        if (url == (this.href)) {
            console.log('this is', $(this));
            console.log('this.closest(li) is', $(this).closest('li'));
            $(this).closest("li").addClass("active");
            //$(this).closest("li").parent().parent().addClass("active");
        }
    });
});

You may also have a problem because adding the hash # target doesn't reload the page so it doesn't re-run your function.
In reality I, personally, would handle this completely differently — I'd add a click handler to the list items; the handler receives an event parameter, and you can use the event.target to add the "active" class to the one list item that was clicked.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!