Reputation: 1
I've tried coding a function that checks if other tabs are active, and if they are it should close them and open another tab, and if not it should just open the tab. I'm new at javascript and tried this but it didn't work.
<div class="showRatings">
<li role="presentation id="productInfomation" class="active">...</li>
<li role="presentation id="productDiscount">...</li>
<li role="presentation id="productRatings">...</li>
$('div.showRatings').click(function(){
if ( document.getElementById('productInfomation').classList.contains('active') ) {
document.getElementById('productInfomation').classList.remove('active');
document.getElementById('productRatings').classList.add('active');
} else if ( document.getElementById('productDiscount').classList.contains('active') ) {
document.getElementById('productDiscount').classList.remove('active');
document.getElementById('productRatings').classList.add('active');
} else {
document.getElementById('productRatings').classList.add('active');
}});
Upvotes: 0
Views: 201
Reputation: 21514
You don't need to write code for each element separately, or even bother to test which tab was previously active. Just remove the "active" class from all of them in one go, and then add it to the element that was clicked:
$('.showRatings li').on("click", function() {
$('.showRatings li.active').removeClass('active');
$(this).addClass('active');
}
Upvotes: 1