Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the current tab in jQuery UI tabs

I am using jQuery UI Tabs inside of the jQuery UI dialog window.

I've come across an instance, where I need to find the id of the current tab when clicking on one of the dialog buttons. Looking at the HTML generated by jQuery UI tabs and dialog, I can't really find a way of doing this. The <ul> elements that hold the tab, are about 3 <div>'s away from the group of dialog buttons.

I tried:

$("#DialogBox").dialog({
    autoOpen: false,
    modal: true,
    buttons: [
        {
        text: "Save",
        click: function () {
        var currentTabId = $(this).closest("ul").attr("id");
        alert(currentTabId);

But I just get an 'undefined' alert back.

Is there a way of doing this?

Thanks

like image 810
SkyeBoniwell Avatar asked Apr 30 '13 14:04

SkyeBoniwell


People also ask

How to get current tab value in jQuery?

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

How do I go to a specific tab in jQuery?

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

How do you check if a browser tab is currently active or not?

The Page Visibility API: It lets the developers know if the current tab is currently active or not. When the user switches to another tab or minimizes the window, then the pagevisibilitychange event gets triggered. This API added these properties to the document object. document.

How do you active a tab?

With Tab Activate, new tabs open immediately instead of in the background. To override Tab Activate and open a new tab in the background, press the Shift key while opening the new tab. Note: Override does not work for bookmarks. Tab Activate effectively reverses Chrome's new tab focus behavior.


1 Answers

This is what worked for me (jQuery 1.9, jQueryUI 1.10). I have not tested this for earlier versions of jQueryUI, but if you have jQueryUI 1.8 or earlier, instead of 'active' try using 'select'.

// GET INDEX OF ACTIVE TAB
// make sure to replace #tabs with the actual selector
// that you used to create the tabs
var activeTabIdx = $('#tabs').tabs('option','active');

// ID OF ACTIVE TAB
// make sure to change #tabs to your selector for tabs
var selector = '#tabs > ul > li';
var activeTabID = $(selector).eq(activeTabIdx).attr('id');

// ID OF ACTIVE TAB CONTENT
// make sure to change #tabs to your selector for tabs
var selector = '#tabs [id=ui-tabs-' + (activeTabIdx + 1) + ']';
var activeTabContentID = $(selector).attr('id');
like image 125
Michael Avatar answered Nov 11 '22 15:11

Michael