I have been trying to go about setting my page's active tab. It is weird, cause I can manually set the active tab like the following:
<div class="tabs">
<ul class="tab-links">
<li id="t1" class="active"><a href="#tab1">TAB</a></li> //setting it here works
<li id="t2"><a href="#tab2">TAB</a></li>
<li id="t3"><a href="#tab3">TAB</a></li>
<li id="t4"><a href="#tab4">TAB</a></li>
<li id="t5"><a href="#tab5">TAB</a></li>
<li id="t6"><a href="#tab6">TAB</a></li>
</ul>
<div class="tab-content">
<div id="tab1" class="tab" style="font-family: 'Open Sans', sans-serif;">
<?php include("tab1.html"); ?>
</div>
<div id="tab2" class="tab" style="font-family: 'Open Sans', sans-serif;">
<?php include("tab2.html"); ?>
</div>
<div id="tab3" class="tab" style="font-family: 'Open Sans', sans-serif;">
<?php include("tab3.php"); ?>
</div>
<div id="tab4" class="tab" style="font-family: 'Open Sans', sans-serif;">
<?php include("tab4.php"); ?>
</div>
<div id="tab5" class="tab" style="font-family: 'Open Sans', sans-serif;">
<?php include("tab5.html"); ?>
</div>
<div id="tab6" class="tab" style="font-family: 'Open Sans', sans-serif;">
<?php include("tab6.html"); ?>
</div>
</div>
However, when I try to set it like this nothing happens:
<script language="javascript" type="text/javascript">
//setting the tabIndex to the stored value.
$(".tab-links").tabs({active: tabIndex}); //need to change this somehow
var tabIndex = parseInt(localStorage.getItem('activeTab')) + 1;
console.log("local storage value of tabIndex parseInt: " + tabIndex);
if(tabIndex != null){
console.log("I am in the if statement: " + localStorage.getItem('activeTab'));
$(document).on("click", ".tabs > ul > li:nth-child(" + tabIndex + ") a", function(e) {});
//$(".tabs").tabs({
// active: tabIndex
//});
}
//storing the last tab index before page refresh
$(document).on("click", ".tab-links a", function(e) {
$('.active').removeClass('active');
$(this).parent().addClass('active');
var curTab = $('.tab-links').find('.active')[0].id;
console.log("This is the currentTab value: " + curTab.replace ( /[^\d.]/g, '' ));
var curTabIndex = (curTab.replace ( /[^\d.]/g, '' ) - 1);
localStorage.setItem('activeTab', curTabIndex);
});
</script>
Am I missing something here? I just need to somehow set the tabIndex to bee the active tab on page refresh.
I have been trying to go about setting my page's active tab. It is weird, cause I can manually set the active tab like the following:
<div class="tabs">
<ul class="tab-links">
<li id="t1" class="active"><a href="#tab1">TAB</a></li> //setting it here works
<li id="t2"><a href="#tab2">TAB</a></li>
<li id="t3"><a href="#tab3">TAB</a></li>
<li id="t4"><a href="#tab4">TAB</a></li>
<li id="t5"><a href="#tab5">TAB</a></li>
<li id="t6"><a href="#tab6">TAB</a></li>
</ul>
<div class="tab-content">
<div id="tab1" class="tab" style="font-family: 'Open Sans', sans-serif;">
<?php include("tab1.html"); ?>
</div>
<div id="tab2" class="tab" style="font-family: 'Open Sans', sans-serif;">
<?php include("tab2.html"); ?>
</div>
<div id="tab3" class="tab" style="font-family: 'Open Sans', sans-serif;">
<?php include("tab3.php"); ?>
</div>
<div id="tab4" class="tab" style="font-family: 'Open Sans', sans-serif;">
<?php include("tab4.php"); ?>
</div>
<div id="tab5" class="tab" style="font-family: 'Open Sans', sans-serif;">
<?php include("tab5.html"); ?>
</div>
<div id="tab6" class="tab" style="font-family: 'Open Sans', sans-serif;">
<?php include("tab6.html"); ?>
</div>
</div>
However, when I try to set it like this nothing happens:
<script language="javascript" type="text/javascript">
//setting the tabIndex to the stored value.
$(".tab-links").tabs({active: tabIndex}); //need to change this somehow
var tabIndex = parseInt(localStorage.getItem('activeTab')) + 1;
console.log("local storage value of tabIndex parseInt: " + tabIndex);
if(tabIndex != null){
console.log("I am in the if statement: " + localStorage.getItem('activeTab'));
$(document).on("click", ".tabs > ul > li:nth-child(" + tabIndex + ") a", function(e) {});
//$(".tabs").tabs({
// active: tabIndex
//});
}
//storing the last tab index before page refresh
$(document).on("click", ".tab-links a", function(e) {
$('.active').removeClass('active');
$(this).parent().addClass('active');
var curTab = $('.tab-links').find('.active')[0].id;
console.log("This is the currentTab value: " + curTab.replace ( /[^\d.]/g, '' ));
var curTabIndex = (curTab.replace ( /[^\d.]/g, '' ) - 1);
localStorage.setItem('activeTab', curTabIndex);
});
</script>
Am I missing something here? I just need to somehow set the tabIndex to bee the active tab on page refresh.
Share Improve this question edited Aug 15, 2017 at 23:24 tiger_groove asked Aug 15, 2017 at 23:13 tiger_groovetiger_groove 1,0163 gold badges22 silver badges51 bronze badges 2-
Where did you get
.tabs()
from? – Mikey Commented Aug 15, 2017 at 23:16 - I was looking online they were saying that is how you set an active tab with the $().tabs({active:1}) – tiger_groove Commented Aug 15, 2017 at 23:20
3 Answers
Reset to default 2Here's one way without using any jQuery libraries except for jQuery.
We use localStorage to store the #hash. Upon refresh of the page, we check if there is a hash and use it to find the link that uses it.
$(function () {
$(document).on('click', '.tab-links a', function () {
var link = $(this),
listItem = link.parent(),
content = $(link.attr('href'));
// show current link and content
listItem.add(content).addClass('active');
// hide other links and contents
listItem.siblings().add(content.siblings()).removeClass('active');
// save selected tab e.g. #tab1. #tab2, ...
localStorage.setItem('activeTab', link.attr('href'));
});
// go back to last tab if any
var activeTab = localStorage.getItem('activeTab'); // ex. #tab1, #tab2, ...
if (activeTab) {
$('.tab-links a[href="' + activeTab + '"]').click();
}
})
Ensure that the content is shown only when active
.tab {
display: none;
}
.tab.active {
display: block;
}
Ensure that the first tab is active by default
<li id="t1" class="active"><a href="#tab1">TAB</a></li>
<div id="tab1" class="tab active" style="font-family: 'Open Sans', sans-serif;">
I haven't read any documentation from where the tab function is ing from since you did not specify but like anything else, this is hackable. All you need to do is to move the CSS class active when you click a tab.
The following JQuery code might work for you:
$('ul.tab-links li').on('click', function(){
$('li.active').removeClass('active'); // to remove the current active tab
$(this).addClass('active'); // add active class to the clicked tab
});
Edit
Also in the docs, it mentions that if you want to set the active tab after initialization you could do so with
$('.selector').tabs("option", "active", 1)
So in your case:
$(".tab-links").tabs("option", "active", tabIndex);
If I'm reading the docs correctly, the tabs should be initialized using the outer div:
$('.tabs').tabs({active: 1});
Also, it looks like the tab-content divs should be on the same level as the ul element.
Here is the sample code they used:
<div id="tabs">
<ul>
<li><a href="#fragment-1">One</a></li>
<li><a href="#fragment-2">Two</a></li>
<li><a href="#fragment-3">Three</a></li>
</ul>
<div id="fragment-1">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</div>
<div id="fragment-2">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</div>
<div id="fragment-3">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</div>
</div>