Skip to content
Related Articles

Related Articles

Improve Article

How to remove active nav-tab when click outside of nav-tab in Bootstrap ?

  • Last Updated : 31 Jul, 2019

Bootstrap has the class called “collapse navbar-collapse” which collapses the navigation bar when the user changes the screen resolution. The java-script along-with it at the bottom of the code triggers the collapsed menu in such a way that when the user clicks the hamburger icon of the menu and navigates to the required link, it again collapses the menu after the click. The scroll event works only for scrollable elements and also for the browser window. It basically attaches a function to run which says that the navbar should collapse when a scroll event occurs, here, it being the navigation of user to different links on the navbar.

Example:




<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>Navigation Bar</title>
  
    <meta charset="utf-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1">
  
    <link rel="stylesheet"
        href=
  
    <script src=
    </script>
  
    <script src=
    </script>
  
    <script src=
    </script>
</head>
  
<body>
  
    <div class="container">
  
        <h1 style="color:green;text-align:center;"
            GeeksforGeeks 
        </h1>
  
        <h2 style="text-align:center;">Collapsing Navbar</h2>
  
        <nav class="navbar navbar-expand-sm bg-success navbar-light">
  
            <!-- Brand/logo -->
            <a class="navbar-brand" href="#">
                <img src=
                    alt="logo"
                    style="width:40px;">
            </a>
  
            <button class="navbar-toggler"
                    type="button"
                    data-toggle="collapse"
                    data-target="#collapse_Navbar">
              
                <span class="navbar-toggler-icon"></span>
            </button>
  
            <div class="collapse navbar-collapse"
                id="collapse_Navbar">
                <ul class="navbar-nav">
                    <li class="nav-item active">
                        <a class="nav-link"
                        data-toggle="tab"
                        href="#home">
                        Home
                    </a>
                    </li>
  
                    <li class="nav-item">
                        <a class="nav-link"
                        data-toggle="tab"
                        href="#algo">
                        Algo
                    </a>
                    </li>
  
                    <li class="nav-item">
                        <a class="nav-link"
                        data-toggle="tab"
                        href="#ds">
                        DS
                    </a>
                    </li>
  
                    <li class="nav-item">
                        <a class="nav-link"
                        data-toggle="tab"
                        href="#lang">
                        Languages
                    </a>
                    </li>
                </ul>
            </div>
        </nav>
    </div>
  
    <!-- Tab panes -->
    <div class="tab-content">
        <div class="tab-pane"
            id="home"
            role="tabpanel">A</div>
        <div class="tab-pane"
            id="algo"
            role="tabpanel">B</div>
        <div class="tab-pane"
            id="ds"
            role="tabpanel">C</div>
        <div class="tab-pane"
            id="lang"
            role="tabpanel">D</div>
    </div>
  
    <script>
$(document).on('click', '.nav-link.active', function() {
  var href = $(this).attr('href').substring(1);
  //alert(href);
  $(this).removeClass('active');
  $('.tab-pane[id="' + href + '"]').removeClass('active');
  
});
$(document).mouseup(function(e) {
    var container = $("#tablist"); // target ID or class
    // if the target of the click isn't the container nor a descendant of the container
    if (!container.is(e.target) && container.has(e.target).length === 0) {
        // get Event here
        $('.active').removeClass('active');
    }
});
    </script>
</body>
  
</html>                    

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :