Alex
Alex

Reputation: 1309

Bootstrap tabs tab('show') refuses to work

Good day,

I am creating a page with bootstrap tabs (BS4) in a form.

Now I need to have a forward and a back button form navigation and I want to use BS4's tab('show') functionality.

However even in a proof of concept I fail to get it to work. I wonder what I have done wrong.

I checked if all Id's are set and if everything is matching. So far everything is correct. Console log does not give me any errors or whatsoever.

I hope I am making a simple bug and this is not a bootstrap issue.

Below my proof of concept code :

My functions.js file's contents :

function activaTab(tab) {
    var tabID = '#' + tab;
    console.log('You just clicked on a function to activate tab : ' + tabID);

    if ($(tabID).length == 0) {
        console.log('But I have to inform you of the misfortunate event that the tab ID you want to pick strangely does not exist');
    }

    $('.nav-tabs a[href="' + tabID + '"]').tab('show'); // Select tab by name
};

My HTML file's contents:

function activaTab(tab) {
    var tabID = '#' + tab;
    console.log('You just clicked on a function to activate tab : ' + tabID);

    if ($(tabID).length == 0) {
        console.log('But I have to inform you of the misfortunate event that the tab ID you want to pick strangely does not exist');
    }

    $('.nav-tabs a[href="' + tabID + '"]').tab('show'); // Select tab by name
};
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">	
	<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
		<li class="nav-item">
			<a class="nav-link active" id="pills-home-tab" data-toggle="pill" href="#pills-home" role="tab" aria-controls="pills-home" aria-selected="true">Home</a>
		</li>
		<li class="nav-item">
			<a class="nav-link" id="pills-profile-tab" data-toggle="pill" href="#pills-profile" role="tab" aria-controls="pills-profile" aria-selected="false">Profile</a>
		</li>
		<li class="nav-item">
			<a class="nav-link" id="pills-contact-tab" data-toggle="pill" href="#pills-contact" role="tab" aria-controls="pills-contact" aria-selected="false">Contact</a>
		</li>
	</ul>
	<div class="tab-content" id="pills-tabContent">
		<div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">..home.<br />
			<button class="btn btn-lg btn-primary" onclick="activaTab('pills-profile');">go to profile</button>    
		</div>
		<div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">..profile.<br />
			<button class="btn btn-lg btn-primary" onclick="activaTab('pills-contact');">go to contact</button>    
		</div>
		<div class="tab-pane fade" id="pills-contact" role="tabpanel" aria-labelledby="pills-contact-tab">..contact.</div>
	</div>
</div>

<!DOCTYPE html>
<html>
<head>
    <title>activate tab</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="  crossorigin="anonymous"></script>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    <script src="functions.js"></script>
</head>
<body>
<div class="container"> 
    <ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
        <li class="nav-item">
            <a class="nav-link active" id="pills-home-tab" data-toggle="pill" href="#pills-home" role="tab" aria-controls="pills-home" aria-selected="true">Home</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" id="pills-profile-tab" data-toggle="pill" href="#pills-profile" role="tab" aria-controls="pills-profile" aria-selected="false">Profile</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" id="pills-contact-tab" data-toggle="pill" href="#pills-contact" role="tab" aria-controls="pills-contact" aria-selected="false">Contact</a>
        </li>
    </ul>
    <div class="tab-content" id="pills-tabContent">
        <div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">..home.<br />
            <button class="btn btn-lg btn-primary" onclick="activaTab('pills-profile');">go to profile</button>    
        </div>
        <div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">..profile.<br />
            <button class="btn btn-lg btn-primary" onclick="activaTab('pills-contact');">go to contact</button>    
        </div>
        <div class="tab-pane fade" id="pills-contact" role="tabpanel" aria-labelledby="pills-contact-tab">..contact.</div>
    </div>
</div>
</body>
</html>

edit:

A very tired mind caused this..

$('.nav-tabs a[href="' + tabID + '"]').tab('show'); // Select tab by name

to be changed in :

$('.nav-pills a[href="' + tabID + '"]').tab('show'); // Select tab by name

Upvotes: 1

Views: 4569

Answers (1)

Sorix
Sorix

Reputation: 918

In your activaTab() method, you are calling tab('show') on an element having the class name nav-tabs, but the element you wish to display has the class nav-pills. The correct call should be:

$('.nav-pills a[href="' + tabID + '"]').tab('show'); // Select tab by name

Upvotes: 1

Related Questions