tags:

views:

822

answers:

1

I've read through the documentation for Jquery UI Tabs (1.7) and I believe I'm a bit in over my head with the Jquery tabs in terms of having specific events happen when a specific tab is clicked.

I have the basic tabs working, and I would simply like to set the focus on a text box when a specific tab is selected.

The UI Docs state you handle select events via:

$('.selector').tabs({
   select: function(event, ui) { ... }
});

I'm not exactly sure what the above means. I've read through a few other examples here at stackoverflow, specficially example. Unfortunately the demo no longer works in the supplied answer.

My Current code for creating and selecting a specific tab using PHP is:

$("#tabs").tabs();
$("#tabs").tabs('option', 'selected', <?php echo $tabID-1; ?>);

Which works fine. I would like to add the ability for when the 4th tab is selected, I set the focus to a text box, something similiar to:

$('#subject').focus();
+2  A: 

What you do is to give the $('.selector').tabs() function an associative array (in JavaScript it is just called object). It is important, that JavaScript is using the fact, that you can assign functions to variables very extensively. If it is easier for you to read and understand you can try this:

var tab_select_function = function(event, ui)
{
    // Objects available in the function context:
    // ui.tab     // anchor element of the selected (clicked) tab
    // ui.panel   // element, that contains the selected/clicked tab contents
    // ui.index   // zero-based index of the selected (clicked) tab
    alert("Tab with index " + ui.index + " clicked!");
};

$('#tabs').tabs({
   select: tab_select_function
});
Daff
Daff,I implemented your suggestion, and I'm heading in the right direction. In the tab_select_function I added a bit of code to grab which tab was selected and for now just alert it. The problem I'm getting is that it is alerting the tab index I am coming from, not the actual tab I just clicked on!var tab_select_function = function(event, ui){var $tabs = $('#tabs').tabs();var selected = $tabs.tabs('option', 'selected');alert(selected);};
Ryan
I copied some description from the documentation and the ui object should actually give as a parameter what you need.
Daff
Thanks a bunch Daff!
Ryan
FYI: Just in case anybody else stumbles upon this: You can't actually set the focus to an input box by using the built in select event for UI Tabs. The select event is fired off before the tab is enabled / shown. Instead of using select event, you need to use the show: event.
Ryan