I have some jQuery tabs on a website and I am trying to make the tabs so they can be linked to directly.
What I mean by this is that, when a user enters a URL that includes the anchors for the tab, then when the page is loaded is should display that tab first.
My original jQuery code was gotten from http://jqueryfordesigners.com/jquery-tabs/ and did not include this ability. Here is the original code:
$(function () {
var tabContainers = $('div.tabs > div');
$('div.tabs ul.tabNavigation a').click(function () {
tabContainers.hide().filter(this.hash).show();
$('div.tabs ul.tabNavigation a').removeClass('selected');
$(this).addClass('selected');
return false;
}).filter(':first').click();
});
I have modified the code to this:
$(function () {
var tabs = [];
var tabContainers = $('div.tabs > div');
$('div.tabs ul.tabNavigation a').each(function () {
if (this.pathname == window.location.pathname) {
tabs.push(this);
tabContainers.push($(this.hash).get(0));
}
});
// sniff for hash in url, and create filter search
var selected = window.location.hash ? '[hash=' + window.location.hash + ']' : ':first';
$(tabs).click(function () {
// hide all tabs
$(tabContainers).hide().filter(this.hash).show();
// set up the selected class
$(tabs).removeClass('selected');
$(this).addClass('selected');
return false;
}).filter(selected).click();
});
This kind of works - when you link to the page with the anchor included in the URL, it brings you to the relevant tab's content, however, and other tabs are visible also (its not hidden the others) and the tab has also not been selected.
You can view an example of this:
No link to second tab, first one shown by default: http://74.54.17.66/~innovarc/improve/success-stories/financial-performance/
Trying to link directly to the second tab: http://74.54.17.66/~innovarc/improve/success-stories/financial-performance/#financial-performance
I have tried to figure out what is wrong with the jQuery code but I cannot figure it out. I would appreciate help with a solution