1

I want to make my css navbar become include in my website. I get some answer in this post: Twitter Bootstrap add active class to li and I used Jquery to make automatically active in my navbar.

and this is the code:

<nav class="navbar navbar-default navbar-fixed-top">
        <div class="container">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
               </div>
            <!-- Collection of nav links and other content for toggling -->
            <div id="navbarCollapse" class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li ><a href="../content/main.php#home">Home</a></li>
                    <li><a href="../content/news.php">News</a></li>
                    <li><a href="../content/contact.php">Contact</a></li>
                    <li class="dropdown" >
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> Information <span class="caret"></span></a>
                      <ul class="dropdown-menu">
                        <li><a href="../content/info.php">Indoor</a></li>
                        <li><a href="../content/info.php#outdoor">Outdoor</a></li>
                      </ul>
                    </li>
                    <li class="dropdown" >
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Gallery<span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="../content/photo.php">Photo</a></li>
                            <li><a href="../content/video.php">Video</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <script type="text/javascript">
        var url = window.location;
        // Will only work if string in href matches with location
        $('ul.nav a[href="'+ url +'"]').parent().addClass('active');

        // Will also work for relative and absolute hrefs
        $('ul.nav a').filter(function() {
            return this.href == url;
        }).parent().addClass('active');
    </script>

my question is how to make the dropdown parent is automatic active when user is in children url? thanks

note: there is one dropdown parent (Gallery Section) that the children did not have same url/link

Community
  • 1
  • 1
Rian
  • 161
  • 3
  • 14

1 Answers1

0
var url = window.location;
$('ul.nav li').removeClass('active'); //remove active class from all li
$('ul.nav a[href="'+ url +'"]').parents('li').addClass('active'); //add active class to current url's parents this will work in dropdown also