I have two tabs and configured usign jQuery UI.
ul class=\"tabs\"
li tabone
li tabtwo
ul
dynamically from C# code behind I will hide
<div id="tabs" style="width: 290px">
<ul >
<li><a id="myTab1" href="#tabs-1" style="color: Green">Báo cáo chuẩn</a></li>
<li><a id="myTab2" href="#tabs-2" style="color: Green">Báo cáo mở rộng</a></li>
</ul>
<div id="tabs-1" style="overflow-x: auto">
<ul class="nav">
<li><a href="@Url.Content("~/Report/ReportDate")"><span class=""></span>Báo cáo theo ngày</a></li>
</ul>
</div>
<div id="tabs-2" style="overflow-x: auto; height: 290px">
<ul class="nav">
<li><a href="@Url.Content("~/Report/PetrolReport#tabs-2")"><span class=""></span>Báo cáo nhiên liệu</a></li>
</ul>
</div>
</div>
var index = $("#tabs div").index($("#tabs-1" ));
$("#tabs").tabs("select", index);
$("#tabs-1")[0].classList.remove("ui-tabs-hide");
As per UI Doc :
First get index of tab which you want to activate.
var index = $('#tabs a[href="'+id+'"]').parent().index();
Activate it
tabs.tabs( "option", "active", index );
Building on @stankovski's answer, a more precise way of doing it which will work for all use cases (for example, when a tab is loading via ajax and so the anchor's href attribute doesn't correspond with the hash), the id in any case will correspond with the li element's "aria-controls" attribute. So for example if you are trying to activate a tab based on the location.hash, which is set to the tab id, 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);
In the case of setting and checking the url hash:
When creating the tabs, use the 'activate' event to set the location.hash to the panel id:
$('#tabs').tabs({
activate: function(event, ui) {
var scrollTop = $(window).scrollTop(); // save current scroll position
window.location.hash = ui.newPanel.attr('id');
$(window).scrollTop(scrollTop); // keep scroll at current position
}
});
Then use the window hashchange event to compare the location.hash to the panel id (do this by looking for the li element's aria-controls attribute):
$(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 where tabs use ajax. Also if you have nested tabs, it isn't too difficult to handle that either using a little more logic.
Active 1st tab
$("#workflowTab").tabs({ active: 0 });
Active last tab
$("#workflowTab").tabs({ active: -1 });
Active 2nd tab
$("#workflowTab").tabs({ active: 1 });
Its work like an array
I found this works more easily than getting an index. For my needs, I am selecting a tab based off a url hash
var target = window.location.hash.replace(/#/,'#tab-');
if (target) {
jQuery('a[href='+target+']').click().parent().trigger('keydown');
}
This is the answer
var index = $('#tabs a[href="#simple-tab-2"]').parent().index();
$("#tabs").tabs("option", "active", index);