Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch to selected tab by name in Jquery-UI Tabs

Tags:

tabs

jquery-ui

If I have three tabs:

<div id="tabs">     <ul>         <li><a href="#sample-tab-1"><span>One</span></a></li>         <li><a href="#sample-tab-2"><span>Two</span></a></li>         <li><a href="#sample-tab-3"><span>Three</span></a></li>     </ul> </div> 

I would like to swap to #sample-tab-2 by it's name. I know I can switch to a tab if I know it's number, but in the case I've run into I won't know that.

Notes: JQuery 1.3.1 / JQuery-UI 1.6rc6

like image 468
Rob Avatar asked Feb 23 '09 16:02

Rob


People also ask

How to get selected tab name in jQuery?

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

How to switch tab using jQuery?

Fortunately, jQuery UI tabs do have a function-thing that can be called to switch tabs. We can bind it to text links to accomplish switching tabs: $('#my-text-link'). click(function() { // bind click event to link $tabs.

How do I go to a specific tab in jquery?

tabs(); $('. benefitc'). click(function () { // bind click event to link $tabs. tabs('select', 4); // switch to tab return false; });

How to get selected tab index in jQuery?

click('tabsselect', function (event, ui) { var selectedTab = $("#tabs"). tabs('option', 'active'); $("#hidLastTab"). val(selectedTab); });


2 Answers

I could not get the previous answer to work. I did the following to get the index of the tab by name:

var index = $('#tabs a[href="#simple-tab-2"]').parent().index(); $('#tabs').tabs('select', index); 
like image 181
Christian George Avatar answered Oct 03 '22 22:10

Christian George


It seems that using the id works as well as the index, e.g. simply doing this will work out of the box...

$("#tabs").tabs("select", "#sample-tab-1"); 

This is well documented in the official docs:

"Select a tab, as if it were clicked. The second argument is the zero-based index of the tab to be selected or the id selector of the panel the tab is associated with (the tab's href fragment identifier, e.g. hash, points to the panel's id)."

I assume this was added after this question was asked and probably after most of the answers

like image 20
Eran Medan Avatar answered Oct 04 '22 00:10

Eran Medan