4

I'm using http://jqueryui.com/demos/tabs/#manipulation. I want to get an title of current selected tab which I earlier named (e.g. from a href). How to get it?

I tried: $(ui.tab).attr('href')

beetle
  • 193
  • 2
  • 4
  • 11

6 Answers6

17

Alternative way to get tab title:

var selected = $("#tabs").tabs( "option", "selected" );
var selectedTabTitle = $($("#tabs li")[selected]).text();
Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87
Gerson Beltrán
  • 402
  • 4
  • 8
  • chessBoard.js:91 Uncaught TypeError: $(...).tabs is not a function How can this possibly work? What is this element with id "tabs"? – Marc Jun 14 '19 at 13:21
7

From the jquery docs,

var selectedTabTitle = null;
$( ".selector" ).tabs({
   select: function(event, ui) {
            selectedTabTitle = $(ui.tab).text();
            alert(selectedTabTitle);
    }
});
  • 1
    Came across this by accident, just a note to future readers, there is a long post about this kind of thing [here](http://stackoverflow.com/questions/300078/jquery-ui-tabs-get-currently-selected-tab-index/7967944#7967944) – SpYk3HH Apr 16 '13 at 15:37
  • 2
    As of JQuery UI 1.9+, you need to use ui.newTab, i.e: `selectedTabTitle = $(ui.newTab).text();` – Gavin Coates Apr 13 '15 at 19:53
5

Use following in case of jQuery 1.9+,

var currentTabTitle = $('div[id="mytabs"] ul .ui-tabs-active > a').attr("href");
Santosh
  • 2,430
  • 5
  • 33
  • 47
3

Just another version:

$("#tabsId .ui-state-active > a").html()
MCurbelo
  • 4,097
  • 2
  • 26
  • 21
3

i guess jquery was modified because now i was able to fetch tab name using:

$(function () {
 $( "#tabs" ).tabs({
    activate : function (event,ui) {
        selectedTabTitle = ui.newTab[0].innerText;
        alert(selectedTabTitle);
    }
});
});
ufk
  • 30,912
  • 70
  • 235
  • 386
0

Thanks I was struggling with this code.

Now I've used thiscode in my program. Working like this.

$('#tabs').click('tabsselect', function (event, ui) {
    var selectedTab = $("#tabs").tabs('option','selected');
    alert("selectedTab===>" +  $($("#tabs li")[selectedTab]).text());
});
Arghya C
  • 9,805
  • 2
  • 47
  • 66