Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change menu color item when active

EDIT: It is working with this code:

        <div id="navigation">
            <ul class="menu">
                <li><a href="/index.php">Home</a></li>
                <li><a href="/about">About Me</a></li>
                <li><a href="/blog">Blog</a></li>
                <li><a href="/projects">Projects</a></li>
                <li><a href="/contact">Contact</a></li>
                <li><a href="/time">Current Time</a></li>
                <li><a href="/help">Help Me</a></li>
            </ul>
        </div>

And the code to make the class active:

        <script>
            $(function () {
                var url = window.location.pathname,
                    urlRegExp = new RegExp(url.replace(/\/$/, '') + "$");
                    $('#navigation a').each(function () {
                    if (urlRegExp.test(this.href.replace(/\/$/, ''))) {
                        $(this).addClass('active');
                    }
                });    
            });
        </script>

This is working now, but when i'm going to Site/Projects/Project1 It makes nothing active. And when i go to Site/Projects it makes the projects tab active.

So it is working, but i also want the projects tab to stay active when i'm on Site/Projects/Project1

like image 202
Mitch Avatar asked Sep 27 '22 17:09

Mitch


1 Answers

Array index starting from zero. But you have started the for loop with count=1. May be that cause the issue. Kindly change the following code and test it out.

for(i=1;i<aObj.length;i++) {

should be

 for(i=0;i<=aObj.length;i++) {
like image 124
Suresh Ponnukalai Avatar answered Oct 03 '22 03:10

Suresh Ponnukalai