Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Selected tab of jQuery UI tabs

The question has been asked a few times before, but none of them are similar to my scenario.

I have jQuery Tabs control that loads my tabs via ajax:

<div id="tabs">
<ul>
    <% if (AccessControll.HasAccess(Url.Action("ViewSchemeInformation","Scheme"))){%>
        <li><a id="tab_1" href="<%= Url.Action("ViewSchemeInformation","Scheme", new {schemeNumber = Model.SchemeNumber}) %>">Information</a></li>
    <%}%>
    <% if (AccessControll.HasAccess(Url.Action("SchemeUpdate", "Scheme"))){%>
        <li><a id="tab_2" href="<%= Url.Action("SchemeUpdate","Scheme", new {schemeNumber = Model.SchemeNumber}) %>">Update</a></li>
    <%}%>
    <%if (AccessControll.HasAccess(Url.Action("MinimumRequirements","Scheme"))){%>
        <li><a id="tab_3" href="<%= Url.Action("MinimumRequirements","Scheme", new {schemeNumber = Model.SchemeNumber}) %>">Minimum Requirements</a></li>
    <%}%>
</ul>

These tabs are shown based on access, so my tab index's are never the same, hence I have added an id to each href.

I link to this specific page from various places and each link must go to this page and select the tab it refers to.

My url will look something like this: http://localhost:34412/Scheme/ViewSchemeDetails/BS-000469800000?activeTab=1

How can I select the tab using jQuery based on the activeTab parameter in my querystring?

Note that number in the querystring always corresponds to the id of my href.

like image 970
stoic Avatar asked Apr 11 '11 09:04

stoic


People also ask

How to set selected tab in jQuery?

var selectedTab = $("#TabList"). tabs(). data("selected. tabs");

What is jQuery ui tab?

jQuery UI tabs() method is used to change the appearance of HTML elements inside the page. This method traverses the HTML code and adds new CSS classes to the element to give them appropriate style.

How to disable tabs in jQuery?

disable( index )Returns: jQuery (plugin only) To disable more than one tab at once, set the disabled option: $( "#tabs" ). tabs( "option", "disabled", [ 1, 2, 3 ] ) .


1 Answers

Get the query string:

var queryString = window.location.search;

Get the activeTab part:

var activeTab = queryString.match(/\bactiveTab=([^&]*)/)[1];

Select the tab with the right ID:

$('#tab_' + activeTab).click();
like image 151
David Tang Avatar answered Sep 21 '22 07:09

David Tang