I am currently working with bootstrap tabs, and I have four of them. My goal is to assign a different color to each tab, which should change when the user clicks on them. Bootstrap's "active" class doesn't seem to be effective in this case because I want each tab to have a unique color. As a solution, I decided to use a jQuery function to add a class when a tab is clicked, but I am unsure of how to remove the class when another tab is clicked.
Is there a way to pass two values to a function – one from one link and another from a different link? For example, when I click on "cars," I want to remove the class from "trucks," and vice versa.
HTML
<ul class="nav nav-tabs" role="tablist">
<li class="active"><a href="#home" role="tab" data-toggle="tab">Link 1</a></li>
<li><a href="#link2" role="tab" data-toggle="tab">Link 2</a></li>
<li><a href="#link3" role="tab" data-toggle="tab" id="cars">Link 2</a></li>
<li><a href="#link4" role="tab" data-toggle="tab" id="trucks" >Link 3</a></li>
</ul>
CSS
#cars
{
color: #FFF;
background: #ec7501;
background: -moz-linear-gradient(top, #ec7501 1%, #c46200 56%, #ec7501 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(1%,#ec7501), color-stop(56%,#c46200), color-stop(100%,#ec7501));
background: -webkit-linear-gradient(top, #ec7501 1%,#c46200 56%,#ec7501 100%);
background: -o-linear-gradient(top, #ec7501 1%,#c46200 56%,#ec7501 100%);
background: -ms-linear-gradient(top, #ec7501 1%,#c46200 56%,#ec7501 100%);
background: linear-gradient(to bottom, #ec7501 1%,#c46200 56%,#ec7501 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ec7501', endColorstr='#ec7501',GradientType=0 );
border-top: 2px solid #de9d5b;
border-bottom: 2px solid #a64800;
border-top-left-radius: 12px;
border-top-right-radius: 12px;
}
.activeTab
{
color:#000 !important;
background:#FFF !important;
}
JS edits based on Ed Cottrell comments,
('#cars, #trucks').on('click', function() {
changeColor($(this)); // passes the element itself
});
function changeColor($elem)
{
$('.activeTab').removeClass('activeTab'); // remove the class from elements that have it
$elem.addClass('activeTab');
}