I have this code for now aiming to create tabs:
<ul class="nav nav-tabs">
<li class="active">
<a href="#custom-tab-0-8cf04a9734132302f96da8e113e80ce5" data-toggle="tab">Home</a>
</li>
<li>
<a href="#custom-tab-0-cce99c598cfdb9773ab041d54c3d973a" data-toggle="tab">Profile</a>
</li>
</ul>
<div class="tab-content">
<div id="custom-tab-0-8cf04a9734132302f96da8e113e80ce5" class="tab-pane active" tabindex="-1">
Content goes here<br>
</div>
<div id="custom-tab-0-cce99c598cfdb9773ab041d54c3d973a" class="tab-pane" tabindex="-1">
Nothing comes here<br>
</div>
</div>
CSS:
ul.nav.nav-tabs {
list-style: none;
margin: 0;
padding: 0;
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
margin-top: 2%;
}
ul.nav.nav-tabs li {
float: left;
}
ul.nav.nav-tabs a {
display: inline-block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
li.active {
background-color: #ccc;
}
How can I create multiple tabs with jQuery, display the content of the the TAB that I click only and add a class active to it?
Note: I cannot change much on the html structure because it is a WordPress site and I used short-code to generate the tabs. I simply styled and tried the jQuery.
For now I can see all the content of all tabs at once.But what I want is only one tab active and the others closed. When I click on a next tab,it should display the content of that tab online,have the class active and deactivate the class active that the previous active tab had.
I started using this code just to test functionality when I click on each TAB but I don't think it is a good practice to use child selectors cause I might have more than 2 tabs:
$(document).ready(function() {
$("ul.nav.nav-tabs li:first-child a").click(function(){
alert("You clicked on the first-child");
});
$("ul.nav.nav-tabs li:nth-child(2) a").click(function(){
alert("You clicked on the second child");
});
});
Thanks in advance.