Get the ID of the tab (div) being activated
Asked Answered
D

11

42

I'm using jquery 1.9 and jquery UI 1.10

I want to be able to get the tab ID when clicking on a tab. For example if I clicked tab named "Second" I want to get "tabs-2" response.

I've done the below code so far:

<script type="text/javascript">
    $(function () {
        $("#tabs").tabs({
            beforeActivate: function (event, ui) {
                alert(/* the id of the tab (div) being activated */);
            }
        });
    });
</script>

<div id="tabs">
    <ul>
       <li><a href="#tabs-1">First</a></li>
       <li><a href="#tabs-2">Second</a></li>
    </ul>
    <div id="tabs-1">
        <p>abcde</p>
    </div>
    <div id="tabs-2">
        <p>fghi</p>
    </div>
</div> 
Dharma answered 24/1, 2013 at 13:11 Comment(1)
time to update the chosen answer I believe :)Omen
M
59

Tested and working with JQueryUI 1.10, how to get the ID of the tab as it is selected:

$("#tabs").tabs({
  beforeActivate: function (event, ui) {
    alert(ui.newPanel.attr('id'));
  }
});
Moats answered 24/2, 2013 at 8:23 Comment(3)
alert(ui.newTab.context.innerText) to display the tab clicked on's textOmen
updated the correct answer because I think this is a better solution. Thanks LamahDharma
just to know... is it wrong to use the way @AdrienBe suggests?Fugere
H
22
var $tabs = $("#tabs").tabs({
    select: function( evt, ui ) {
        $("#current").text( $(ui.tab).attr('href'));
    }
})

UPDATE - Another solution for jquery UI 1.10

    $(function () { $('#tabs').tabs({ 
             activate: function (event, ui) {
             var active = $("#tabs").tabs("option", "active");
             alert($("#tabs ul>li a").eq(active).attr('href')); 
     } 
}); 

Updated jsFiddle Demo

Harassed answered 24/1, 2013 at 13:17 Comment(4)
The code you provide is not fully compatible with jquery ui 1.10. Anyway I want to get the data by clicking on the tab, not another button on the page. thanks anywayDharma
The "Updated jsFiddle Demo" shows the same demo, tabs and a external button which shows the selected tab id. As I said, I want to get the id by clicking on the tab, not another button on the page.Dharma
@Dharma - both actions will do the same :) , when you click on the tab you will get tab Id also. - This updated one without button - jsfiddle.net/WnDV9/3Harassed
yes, you're right, sorry. But I can not run your code as you put it, since it seems that in version 1.10 doesnt work, because the various changes in that version. But to be fair, your code is what helped me to get a solution. I can not answer myself yet, so if you edit your answer with this code I will give it for good. $(function () { $('#tabs').tabs({ activate: function (event, ui) { var active = $("#tabs").tabs("option", "active"); alert($("#tabs ul>li a").eq(active).attr('href')); } }); });Dharma
G
4

This works for me

$( "#editor_tabs" ).tabs( { 
    activate: function ( event, ui ) {
        $(ui.newTab.find("a").attr("href")).html("Got It");
    }
});
Gaige answered 6/12, 2013 at 6:6 Comment(1)
I use a similar method to make various "flavours" of Tabs.... by adding a class or another attribute, I can have some specific behaviour enabled on activation.Tumbling
U
2

If you want to get something when clicking on tab, use the select event.

$('#tabs').tabs({
  beforeActivate: function(event, ui){
     alert($(ui.tab).attr('href'));
 }
 });

EDIT

Changed 'select' to 'beforeActivate' as it has been removed from version 1.10

Unsought answered 24/1, 2013 at 13:21 Comment(3)
thanks but select event has been removed in jquery ui 1.10 jqueryui.com/upgrade-guide/1.10/…Dharma
thanks man.. But if you wanted to know how to get the href, hope "$(ui.tab).attr('href')" would have helped u.Unsought
it should be: $(ui.newTab).attr('href') -> api.jqueryui.com/tabs/#event-beforeActivate anyway, it returns nothingDharma
D
1

I'm guessing you want to know which tab gets activated, and based on that execute various actions.

Rather than fetching ids and attributes from the HTML elements, here is how you do it:

$("#tabs").on("tabsactivate", (event, ui) => {
    if (ui.newPanel.is("#tabs-1")){
        //first tab activated
    }
    else{
        //second tab activated
    }
});

The activate event is not getting called when the tabs get created. For that you'd need to add the "tabscreate" event in the mix, but you get the idea.

Dionysus answered 28/9, 2013 at 0:36 Comment(0)
E
1

Examine the JQueryUI event object (use a browser debugger like Mozilla's Firebug or Chrome developer tools).

$("#tabs").tabs({        
    activate: function( event, ui ) { 
        console.log(event)  //unnecessary, but it's how to look at the event object              
        alert(event.currentTarget.hash)
    }
});
Employment answered 14/10, 2013 at 5:1 Comment(0)
C
1
var id=$("ulselector li.ui-state-active").attr("aria-controls"));
alert(id);
Commissionaire answered 20/10, 2014 at 6:58 Comment(0)
S
0

Use aria-controls

alert(ui.newTab.attr("aria-controls"));
Swinger answered 27/1, 2013 at 12:0 Comment(0)
I
0

In my opinion, the easiest way is to add data- to the <li ...> that make up the tabs.

For example:

                <li data-index="2" data-tabname="Notes">
                  <a href="#tabs-Employee-Notes">Notes</a>
                </li>

Then handle the beforeActivate event (if that is the event you are looking at):

$("#jqTabs_EmployeeDetails").tabs({
    heightStyle: "fill",
    beforeActivate: function (event, ui) {
        var n = ui.newTab;

        console.log($(n).data());
    }
});

For example, $(n).data("tabname") would return the tab name. This also allows you to add any other information you need to the tabs. Simplist solution and scalable I believe.

Impregnate answered 13/1, 2015 at 3:53 Comment(0)
R
0

It works for me

$('#divName .ui-tabs-panel[aria-hidden="false"]').prop('id');
Royce answered 11/3, 2015 at 5:52 Comment(1)
Add some explanation to your answer please.Nissie
S
0

The easyest way I found is :

$('#tabs .ui-state-active').attr('aria-controls')

This will return the ID from the div witch is active. Remember you must change #tabs to your jquery.tabs ID.

Sherilyn answered 20/1, 2016 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.