yando18
asked on
Help With URL parameter to open tabs info
Need some help with the script below. I would like to add the ability to pass a url parameter that would open one of the tabs based on ex URL#tabinfo parameter. Any help would be great thanks.
(function ($) {
function getactTabAnc() {
return this.find('.active>a')[0];
}
function getContentId(tabAnchorS) {
return $(tabAnchorS).attr('href').replace('#', '');
}
function applyStyles(newActTabAnc) {
var actTabAnc = newActTabAnc || getactTabAnc.apply(this), activeContentId = getContentId(actTabAnc);
this.find('a').each(function () {
var $cur = $(this), curContentId = getContentId(this);
if (activeContentId === curContentId) {
$cur.closest('li').addClass('active');
$('#' + curContentId).show();
}
else {
$cur.closest('li').removeClass('active');
$('#' + curContentId).hide();
}
});
}
$.fn.tabs = function () {
return this.each(function () {
var $tabTainer = $(this);
applyStyles.apply($tabTainer);
$tabTainer.find('a').click(function (e) {
console.log('clicked');
var actTabAnc = getactTabAnc.apply($tabTainer), isActive = this === actTabAnc;
e.preventDefault();
if (!isActive) {
applyStyles.apply($tabTainer, [this]);
}
});
});
};
})(jQuery);
<ul id="tabsHolder">
<li class="active"><a href="#tabId1">Header 1</a></li>
<li><a href="#tabId2">Header 2</a></li>
</ul>
<div id="tabId1">Content 1</div>
<div id="tabId2">Content 2</div>
ASKER
How would you apply this to the script attached?
what is the relation between the parameter value and tab ?
ASKER
Ideally I would like the parameter to open the tab. For example http://URL.com/#tab2 would open up the second tabs content.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So I added that code to the second line however it does not seem to work. When I added #tabId2 to the end of the URL the tab does not open. Thoughts? Thanks again.
ASKER
Does anyone have any thoughts about this issue? I could really use some advice.
ASKER
So I got it to work by using:
$(window.location.hash).pa rent().add Class("act ive");
However I need remove the active class placed on the li by default.
$(window.location.hash).pa
However I need remove the active class placed on the li by default.
ASKER
So I was able to get the tabs to display using the code below, which has been altered a bit. The tabs active state changes however the tabs content is not displaying. Instead the default tabs content is displayed. Any help would be great. Thanks for your help with this.
<script type="text/javascript">
$(document).ready(function () {
var hash = location.href.split("#")[1];
if (hash) {
$('#tabsHolder li').removeClass('active');
$("a[href=#" + hash + "]").parent().addClass("active");
} else {
$("#tabsHolder li:first-child").addClass("active");
}
});
</script>
ASKER
I used another solution. Thanks
http://stackoverflow.com/questions/901115/get-querystring-values-with-jquery
then you can set the tab related to it to active