Trigger a function when a jQuery UI tab is clicked/made active?
Asked Answered
C

4

5

I'm using jQuery tabs and the content in one of the tabs is retrieved via an AJAX call. But I don't want to trigger the call until the tab is clicked (made active) because the user might not necessarily click it. What's the best way to do this?

jQuery UI provides select and show events for the tabs, but these fire when any tab is selected or shown, not just a specific one (as far as I know).

I've also tried assigning a click event to the specific tab's ID:

$("#my-tab").click(function(){
...
     $.post(URL, function(data){
          ...
     });
...
}

But this seemingly has no effect and the call is never triggered.

Congregationalist answered 12/7, 2012 at 17:40 Comment(0)
T
8

Ui-tabs provide a function which is triggered when you select a tab.

$( ".yourTab" ).tabs({
   select: function(event, ui) {
        if(ui.index == myTabIndex) {
            $.post(URL, function(data){
              ...
            });
        }
   }
});

Note Use activate for new versions.

reference

Trencher answered 12/7, 2012 at 17:47 Comment(3)
Worked perfectly! I wasn't aware of how to use select for just a specific tab. Thanks!Congregationalist
@RicardoAlvaroLohmann this works great, but I do not see the select: object in the documentation, was looking there before I found this.Beck
@Beck I think it has been replaced with activate.Trencher
O
17

The ticked solution doesn't work any more because jquery-ui has changed. Also you need to call the tabs function on the entire tab-set, not just one tab. And ui.index needs to be replaced. If the 2nd panel has the id "tab2" then you can use:

$("#tabs").tabs({
    activate: function(event, ui) {
       if (ui.newPanel.selector == "#tab2") {
            alert("tab 2 selected");
       }
    }
});
Orin answered 1/4, 2014 at 5:5 Comment(0)
T
8

Ui-tabs provide a function which is triggered when you select a tab.

$( ".yourTab" ).tabs({
   select: function(event, ui) {
        if(ui.index == myTabIndex) {
            $.post(URL, function(data){
              ...
            });
        }
   }
});

Note Use activate for new versions.

reference

Trencher answered 12/7, 2012 at 17:47 Comment(3)
Worked perfectly! I wasn't aware of how to use select for just a specific tab. Thanks!Congregationalist
@RicardoAlvaroLohmann this works great, but I do not see the select: object in the documentation, was looking there before I found this.Beck
@Beck I think it has been replaced with activate.Trencher
C
1

You may try something like this:

/*tab placeholder*/.tabs({
    select: function(event, ui) {
       if(ui.item.id === "my-tab"){
           //explicitly trigger mouse click on tab
           $(ui.item).trigger('click');
       }
    }
});
//------------------
$("#my-tab").click(function(){
    alert('something')
});
Coacher answered 12/7, 2012 at 17:46 Comment(0)
K
1

The select event has a ui parameter where you can check the index of the selected tab. This SO question has many examples: jQuery UI Tabs Get Currently Selected Tab Index

You can then do the ajax call when you have checked the index of the tab.

EDIT: You also have many examples in the official Jquery UI doc here: http://docs.jquery.com/UI/Tabs

Kevenkeverian answered 12/7, 2012 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.