Close active nav-tab when nav-link is clicked and when click is done anywhere on the page.
I've tried changing the removeClass from .tab-pane to every possible variation i.e. tabpanel, nav-item, nav-link and just tab with no luck.
<!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-js -->$(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');
});
<!-- language: lang-html -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#home" role="tab">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#profile" role="tab">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#messages" role="tab">Messages</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#settings" role="tab">Settings</a>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div class="tab-pane" id="home" role="tabpanel">A</div>
<div class="tab-pane" id="profile" role="tabpanel">B</div>
<div class="tab-pane" id="messages" role="tabpanel">C</div>
<div class="tab-pane" id="settings" role="tabpanel">D</div>
</div>
<!-- end snippet -->
The output should close the tab when nav-link is clicked on the open tab.