views:

2057

answers:

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.

+3  A: 

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);
})
redsquare
That was exactly what I was looking for. Thank you!
rtperson
+1  A: 

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!

willvv
A: 

Hi! I have one question. How can i get the url from a tab?

turbod
Look at redsquare's answer above: you want to use the $('#tabs a') selector.
rtperson
This is return all tabs url, but i need only one tab url.I can dinamicaly change the url or add parameter, but just in one
turbod
To get the first URL: $('#tabs a')[0]
rtperson
+2  A: 
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

Gijserman