Getting Target URL from jQuery-UI Tabs
Asked Answered
R

3

4

I'm working on a project that makes heavy use of jQuery tabs and Ajax. Loading data into the tabs is simplicity itself, but the data in the tabs needs to be filtered by a select box that sits outside the tabs div.

Here's where my problem starts. Let's say my tab makes an Ajax call to the URL "tab1.html." jQuery tabs changes this target into a hash value something like "#ui-tabs-10," but I can get the original URL through the following code:

    $("#tabs").tabs({
        select: function(event,ui) {
            var url = $.data(ui.tab, 'load.tabs');
            ...do stuff with url
        }
    });

But I cannot seem to access the ui.tab object outside this event call. So my select box change event ends up looking like this:

var urls = {
    0 : "tab1.html",
    1 : "tab2.html",
    2 : "tab3.html"
}

$('#selectBox').change(function(){
    var tabs = $("#tabs").tabs();
    var id = $('#selectBox').attr("selectedIndex");
    var selectedTab = tabs("option", "selected");
    var newUrl = urls[selectedTab] + "?id=" + id;
    tabs("url", selectedTab, newUrl);
    tabs("load", selectedTab);
});

My problem is that hash map. I don't need it, and it duplicates info that I've already coded into the tabs div itself.

<div id="tabs">
    <ul>
        <li><a href="tab1.html">tab1</a></li>
        <li><a href="tab2.html">tab2</a></li>
        <li><a href="tab3.html">tab3</a></li>
    </ul>
</div>

I've exhausted both the docs and the DOM tree in Firebug to no avail. Any ideas on how I can retrieve the href from outside a tab event?

I'm using jQuery UI version 1.7.2. Muchos gracias in advance. You guys are the best.

Ravage answered 31/8, 2009 at 20:48 Comment(0)
B
8

The following will write out the href's to the console.

Demo that alerts the ajax tab href's here

$('#tabs a').each(function() {
   var href = $.data(this, 'href.tabs');
   console.log(href);
})
Bounder answered 31/8, 2009 at 21:5 Comment(0)
S
5
var links = $("#thetabs > ul").find("li a");
var url = $.data(links[tabnum], 'href.tabs');

tabnum is the zero based index of the tab you want the url from

Shaunda answered 26/5, 2010 at 14:32 Comment(0)
C
1

You can also set the title property in the tab links.

<div id="tabs">
    <ul>
        <li><a href="tab1.html" title="First tab contents">tab1</a></li>
        <li><a href="tab2.html" title="Second tab contents">tab2</a></li>
        <li><a href="tab3.html" title="Third tab contents">tab3</a></li>
    </ul>
</div>

With this, JQueryUI will create divs with an id based on the title, with the spaces replaced by underscores, therefore you should be able to access the divs using something like

$("#First_tab_contents")

Cheers!

Caliban answered 29/10, 2009 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.