Based on @stankovski's answer, a more accurate way to do this, which will work for all use cases (for example, when a tab is loaded via ajax, and therefore the href attribute of the binding does not match the hash), id will in any case correspond to the aria li attribute attribute -controls ". For example, if you are trying to activate a tab based on location.hash that is set to a tab identifier, then it is better to look for "aria-controls" than for "href".
With jQuery UI> = 1.9:
var index = $('#tabs > ul > li[aria-controls="simple-tab-2"]').parent().index(); $("#tabs").tabs("option", "active", index);
If you install and verify the hash of the URL:
When creating tabs, use the "activate" event to set location.hash to the panel identifier:
$('#tabs').tabs({ activate: function(event, ui) { var scrollTop = $(window).scrollTop();
Then use the hashchange event of the window to compare location.hash with the panel id (do this by looking for the aria-controls attribute of the li element):
$(window).on('hashchange', function () { if (!location.hash) { $('#tabs').tabs('option', 'active', 0); return; } $('#tabs > ul > li').each(function (index, li) { if ('#' + $(li).attr('aria-controls') == location.hash) { $('#tabs').tabs('option', 'active', index); return; } }); });
This will handle all cases, even if the tabs use ajax. In addition, if you have nested tabs, it is not so difficult to handle, or using a little more logic.
JohnRDOrazio Oct 17 '15 at 9:56 2015-10-17 09:56
source share