Home > other >  Bootstrap tabs not changing content
Bootstrap tabs not changing content

Time:06-27

I'm trying to integrate bootstrap tabs in one of our pages. I've copied the example from https://getbootstrap.com/docs/4.0/components/navs/#javascript-behavior but the tabs are not working correctly (they show up correctly but when I click on a tab, nothing happens).

<ul  id="myTab" role="tablist">
    <li  role="presentation">
        <button  id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">Home</button>
    </li>
    <li  role="presentation">
        <button  id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="false">Profile</button>
    </li>
    <li  role="presentation">
        <button  id="contact-tab" data-bs-toggle="tab" data-bs-target="#contact" type="button" role="tab" aria-controls="contact" aria-selected="false">Contact</button>
    </li>
</ul>
<div  id="myTabContent">
    <div  id="home" role="tabpanel" aria-labelledby="home-tab">home</div>
    <div  id="profile" role="tabpanel" aria-labelledby="profile-tab">profile</div>
    <div  id="contact" role="tabpanel" aria-labelledby="contact-tab">contact</div>
</div>

Both bootstrap and jQuery are installed:

"bootstrap": "^4.6.1",
"jquery": "^3.6.0",

and bundled correctly (has other pages use them as well).

What could be the reason that the content isn't changing? Do I have to add JS code manually?

Thanks in advance

Edit: I've tried registering the click events for each tab. When clicking on a tab, it console logs the corresponding message but does not show the correct content...

$(document).on('click', '#home-tab', function (event) {
    event.preventDefault();
    console.log('home clicked')
    $('#myTab a[href="#home"]').tab('show')
})

$(document).on('click', '#profile-tab', function (event) {
    event.preventDefault();
    console.log('profile clicked')
    $('#myTab a[href="#profile"]').tab('show')
})

$(document).on('click', '#contact-tab', function (event) {
    event.preventDefault();
    console.log('contact clicked')
    $('#myTab a[href="#contact"]').tab('show')
}) 

CodePudding user response:

Following the official Bootstrap guide, it looks like you need to add the Javascript code manually:

Enable tabbable tabs via JavaScript (each tab needs to be activated individually):

$('#myTab a').on('click', function (e) {
  e.preventDefault()
  $(this).tab('show')
})

You can activate individual tabs in several ways:

$('#myTab a[href="#profile"]').tab('show') // Select tab by name
$('#myTab li:first-child a').tab('show') // Select first tab
$('#myTab li:last-child a').tab('show') // Select last tab
$('#myTab li:nth-child(3) a').tab('show') // Select third tab

CodePudding user response:

You don't have any Id with the name myTab If I see your HTML correctly I only see this:

<div id="myTabContent">

So I guess it should be $('#myTabContent a[href="#profile"]').tab('show')

  • Related