K
K
Konstantin2018-03-21 12:45:11
JavaScript
Konstantin, 2018-03-21 12:45:11

How to highlight a menu item when I'm on its child page?

There is a navbar

<ul class="sidebar-nav sidebar-nav-icons">
  <li><a href='/'><i></i>bla bla</a></li>
  <li><a href="Shipments"><i></i>bla bla</a></li>
</ul>

The Shipments page has a link to the next page 'Shipments/Shipment' and when I'm on that page I want the Shipment in the main menu to remain highlighted.
My js which highlights only the main page and Shipments:
$(function () {
                    var url = window.location.href;
                    $(".sidebar-nav-icons a").each(function () {
                        if (url == (this.href)) {
                            $(this).closest("li").addClass("active");
                        }
                    });
                });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Gorelov, 2018-03-21
@Kirill-Gorelov

I think you are setting the class incorrectly.
https://jsfiddle.net/gorelov/f4co742r/
I forwarded it here. There, just cross the path and it will be ready.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question