Jack_D
Jack_D

Reputation: 75

How to implement a "next" button using jQuery tabs "Easy Tabs"

This plugin features next and previous buttons in the docs, but mentions no method of implementing them... http://os.alfajango.com/easytabs/#previous-and-next-buttons

Can anyone send me in the right direction?

Upvotes: 0

Views: 1324

Answers (2)

bazeblackwood
bazeblackwood

Reputation: 1204

I liked user1026361's answer. But the default implementation on the site shown left a little to be desired. For example, I just wanted a single pair of previous and next symbols on either side of the tabs, as opposed to a single set in the content of each tab. Here's what I came up with:

JS: (Note, be aware that the prev and next functions can probably be consolidated into one function. They're almost identical :D )

$( document ).ready(function() {
  $("#tab-full-container").easytabs({
    animate: true,
    animationSpeed: 1000,
    defaultTab: "span#tab-1",
    panelActiveClass: "active-content-div",
    tabActiveClass: "selected-tab",
    tabs: ".tab-wrap > div.etabs > span",
    updateHash: false,
    cycle: 5000
  });

  var $tabs = $('.tab');
  var $tabContainer = $('.etab');

  $('.prev-tab').click( function() {
    var currTab, i;
    currTab = $('a.selected-tab').attr('href');
    var i = ( parseInt(currTab.match(/\d+/)) - 2 );
    $tabs.children('a:eq(' + i + ')').click();
  });

  $('.next-tab').click( function() {
    var currTab, i;
    currTab = $('a.selected-tab').attr('href');
    var i = ( parseInt(currTab.match(/\d+/)) );
    $tabs.children('a:eq(' + i + ')').click();
  });

});

HTML:

<div id="tab-full-container" class='tab-container'>
  <div class="tab-wrap">
    <span class="prev-tab ctl">«</span>
    <div class='etabs'>
      <span class='tab' id="tab-1"><a href="#tabs1">&nbsp;</a></span>
      <span class='tab'><a href="#tabs2">&nbsp;</a></span>
      <span class='tab'><a href="#tabs3">&nbsp;</a></span>
      <span class='tab'><a href="#tabs4">&nbsp;</a></span>
      <span class='tab'><a href="#tabs5">&nbsp;</a></span>
      <span class='tab'><a href="#tabs6">&nbsp;</a></span>
      <span class='tab'><a href="#tabs7">&nbsp;</a></span>
      <span class='tab'><a href="#tabs8">&nbsp;</a></span>
    </div>
    <span class="next-tab ctl">»</span>
  </div>
  <div class='panel-container'>

    <div id="tabs1">
    </div>
    <div id="tabs2">
    </div>
    <div id="tabs3">
    </div>

    ...ETC.

  </div>
</div>

I can post the CSS too if you like.

Upvotes: 1

user1026361
user1026361

Reputation: 3657

The code that drives the next and prev buttons, from just under the tabs panel:

$('.next-tab, .prev-tab').click(function() {
  var i = parseInt($(this).attr('rel'));
  var tabSelector = $tabs.children('a:eq(' + i + ')').attr('href');
  $tabContainer.easytabs('select', tabSelector);
  return false;
});

Make sure you note the class and rel of the button element which are vital to the functionality

Upvotes: 1

Related Questions