I have the following code: fiddle
Which works great in websites I create my self and with no JS the tabs act as jump links to the relevant sections. When placed in the bespoke CMS I am forced to use at the moment the jump links don't work. I have tried adding more relative links to the tabs which makes it work with no JS but with JS the tabbed content doesn't show.
Am I missing something?
html:
<ul id="tabs">
<li><a href="#tab1">test1</a></li>
<li><a href="#tab2">test2</a></li>
<li><a href="#tab3">test3</a></li>
<li><a href="#tab4">test4</a></li>
</ul>
<div class="container" id="tab1">Some content</div>
<div class="container" id="tab2">Some content</div>
<div class="container" id="tab3">Some content</div>
<div class="container" id="tab4">Some content</div>
jQuery:
$('#tabs li a:not(:first)').addClass('inactive');
$('.container').hide();
$('.container:first').show();
$('#tabs li a').click(function(){
var t = $(this).attr('href');
$('#tabs li a').addClass('inactive');
$(this).removeClass('inactive');
$('.container').hide();
$(t).fadeIn('slow');
return false;
})
if($(this).hasClass('inactive')){ //this is the start of our condition
$('#tabs li a').addClass('inactive');
$(this).removeClass('inactive');
$('.container').hide();
$(t).fadeIn('slow');
}
I am guessing your website is having problems with href, i presume that when user clicks a href, website automatically eradicating itself.
Here is new solution's jsFiddle.
I have a new solution for you:
updated jQuery:
$('#tabs li a').click(function(){
var t = $(this).attr('id');
if($(this).hasClass('inactive')){ //this is the start of our condition
$('#tabs li a').addClass('inactive');
$(this).removeClass('inactive');
$('.container').hide();
$('#'+ t + 'C').fadeIn('slow');
}
});
new html markup:
<ul id="tabs">
<li><a id="tab1">test1</a></li>
<li><a id="tab2">test2</a></li>
<li><a id="tab3">test3</a></li>
<li><a id="tab4">test4</a></li>
</ul>
<div class="container" id="tab1C">1Some content</div>
<div class="container" id="tab2C">2Some content</div>
<div class="container" id="tab3C">3Some content</div>
<div class="container" id="tab4C">4Some content</div>
$(document).ready(function() {
$("#content div").hide(); // Initially hide all content
$("#tabs li:first").attr("id","current"); // Activate first tab
$("#content div:first").fadeIn(); // Show first tab content
$('#tabs li a').click(function(e) {
e.preventDefault();
if ($(this).attr("id") == "current"){ //detection for current tab
return
}
else{
$("#content div").hide(); //Hide all content
$("#tabs li").attr("id",""); //Reset id's
$(this).parent().attr("id","current"); // Activate this
$( $(this).attr('href')).fadeIn(); // Show content for current tab
}
});
});
See Demo: http://jsfiddle.net/pradeepk00786/5ezT3/
Solution JSFiddle:: https://jsfiddle.net/incorelabs/mg6e4ren/74/
Implementing Tabs is really simple, I have modified the HTML for your question a bit. Removed the anchor tags coz they are not needed.
HTML
<ul>
<li class="tab-switcher" data-tab-index="0" tabindex="0">
Tab 1
</li>
<li class="tab-switcher" data-tab-index="1" tabindex="0">
Tab 2
</li>
<li class="tab-switcher" data-tab-index="2" tabindex="0">
Tab 3
</li>
<li class="tab-switcher" data-tab-index="3" tabindex="0">
Tab 4
</li>
</ul>
<div id="allTabsContainer">
<div class="tab-container" data-tab-index="0">
Some content for Tab - 1
</div>
<div class="tab-container" data-tab-index="1" style="display:none;">
Some content for Tab - 2
</div>
<div class="tab-container" data-tab-index="2" style="display:none;">
Some content for Tab - 3
</div>
<div class="tab-container" data-tab-index="3" style="display:none;">
Some content for Tab - 4
</div>
</div>
HTML De-Mystified
jQuery
$(document).ready(function () {
var previousActiveTabIndex = 0;
$(".tab-switcher").on('click keypress', function (event) {
// event.which === 13 means the "Enter" key is pressed
if ((event.type === "keypress" && event.which === 13) || event.type === "click") {
var tabClicked = $(this).data("tab-index");
if(tabClicked != previousActiveTabIndex) {
$("#allTabsContainer .tab-container").each(function () {
if($(this).data("tab-index") == tabClicked) {
$(".tab-container").hide();
$(this).show();
previousActiveTabIndex = $(this).data("tab-index");
return;
}
});
}
}
});
});
jQuery De-Mystified
If there are doubts or if someone has suggestions, do comment on the post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With