views:

337

answers:

3

I'm using the JQuery tabs plugin.

I'd like to link content within tab 1 to take me to tab 3 instead of having to click Tab 3 to get to it. How do I do this?

@redsquare... So, i modified it based on your suggestion that way I can make all the tabs interact with each other. This change works, thanks again, so I guess I'm wondering if there's a nicer way of writing this or is this the best way?

Demo with latest changes: http://jsbin.com/etoku3/

<script type="text/javascript"> 
    $(document).ready(function() {
        var $tabs = $("#container-1").tabs();
          $('a.tab1').click(function(){$tabs.tabs('select', 0);});
          $('a.tab2').click(function(){$tabs.tabs('select', 1);});
          $('a.tab3').click(function(){$tabs.tabs('select', 2);});
          $('a.tab4').click(function(){$tabs.tabs('select', 3);});
          $('a.tab5').click(function(){$tabs.tabs('select', 4);});
    });
</script>
+3  A: 

You need the Tabs select method

e.g

$('#anchor').click( function(){

  $('#tabs').tabs( "select" , 2 )

});

Demo using your markup here

redsquare
Very interesting! i updated my post to reflect what you suggested. I'm looking to really have all the tabs interact with each other from any tab not just a single tab.
Evan
try http://jsbin.com/etoku3/9
redsquare
@redsquare - by golly i think you got it! now that is some link tab swapping right there!
Evan
a second issue has cropped up, is there a way to make the tab also bring focus to the top of the page? See, in this example, http://jsbin.com/etoku3/11/ when you scroll down and click a link, you'll notice the tab DOES switch, but it doesn't take you to the top of the page to see the TOP of the content.
Evan
A: 

If the links are going to be a common occurrence, following design could be used:

<script lang="text/javascript">
$('.tablink').live( 'click', function(){
  $('#container-1').tabs('select', $(this).attr('rel'));
});
</script>

And in the body have following links:

<span class="tablink" rel="2">Link to tab 2</span>

or

<a href="" class="tablink" rel="2">Link to tab 2</a>
azatoth
that will not work because the tabs widget stops the links from propagating to the body which is where live listens
redsquare
also, in our cms, rel is not available for me to use within a link property.
Evan
redquare solved it!
Evan
A: 

A semantic solution:

$(document).ready(function() {
$('a.directTab').click( function(){
    var tabWanted = $(this).attr('rel').split('-')[1];
    $('#container-1').tabs( "select" , tabWanted);
    });
});

If you have a problem with the "click" alone event, you can use "live".

For the link, you replace the "rel" attribute by the tab wanted:

<a class="directTab" rel="fragment-3">want this text</a> to link to tab 3 

Otherwise try this Obtrusive code:

<a onclick="$('#container-1').tabs('select', 2);">want this text</a> to link to tab 3 
Amirouche Douda