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
Here is one simple method.
This will return the current tab ID.
(This answer is relevant for JQuery UI 1.12 and probably a few versions before.)
It depends what you mean by tab... There is the thing that you click to select a tab and the panel that is displayed because of the click. The thing you click is a list item
<li>
that contains an anchor<a>
tag with anhref
attribute that points to the panel id (it's prepended with a '#'). The panel id and href values are set by you (not JQuery). The list item has no id by default, but the anchor element does... it is generated by JQuery and will be something like 'ui-id-88'. To get either the tab id, anchor id or the panel id, you can use the following:Use following in case of jQuery 1.9+,
Asume that ui tabs container's id is tab-container. Working snippet is
Tested with jQuery UI v1.11.2, jQuery v1.11.3 and Chrome 45.
For JQuery UI 1.11+ this worked for me:
This also works, using JQuery 3.1.1 and Jquery UI 1.12.1, when you need to select your active tab in javascript (by "select" I mean in a JQuery selector, not "select" in the sense of making the tab active, since of course the tab is already active).
To get a reference to the currently selected tab, first get a reference to the active link (substitute the id of your tabs container "myTabs"):
$link has the id the tab in the attribute "aria-controls":
$tab is a reference to the tab body. For example, you could call
to fill or replace the tab content.