Fox
Fox

Reputation: 83

Load content on tab selected (Foundation)

I am using Zurb Foundation 6 Tabs. I have a javascript question. Here is my html for a 3 tab layout.

<ul class="tabs" data-tabs id="myTabs">     
    <li class="tabs-title is-active"><a href="#panel1" aria-selected="true">Tab 1 Info</a></li>
    <li class="tabs-title" ><a href="#panel2">Tab 2 Info</a></li>
    <li class="tabs-title" ><a href="#panel3">Tab 3 Info</a></li>
</ul>

<div class="tabs-content" data-tabs-content="myTabs">
    <div class="tabs-panel is-active" id="panel1">
    ....
    </div>
    <div class="tabs-panel" id="panel2">
    ....
    </div>
    <div class="tabs-panel" id="panel3">
    ....
    </div>
</div>

The tabs work great! However, I want to load content into Tab 3 only when clicked. I will be using ajax to load the content. Foundation 6 docs provide a javascript event that fires when ANY tab is clicked on. See below:

 $('#myTabs').on('change.zf.tabs', function() {
      console.log('Those tabs sure did change!');
 });

I need an event to fire ONLY when panel3 is selected. How to do?

Upvotes: 8

Views: 7782

Answers (6)

Liron Harel
Liron Harel

Reputation: 11247

For Foundation 6 I used:

$("#section-tabs").on('change.zf.tabs', function (event, tab) {
        var tabId = tab.attr("id");
}) 

Upvotes: 0

Aaron McMillin
Aaron McMillin

Reputation: 2667

I'm having some luck with this in Foundation 6:

$('.tabs').on('change.zf.tabs', function(event, tab){
    console.log(tab.children('a').attr('id'));
    console.log(tab.children('a').attr('href'));
});

Upvotes: 0

killebytes
killebytes

Reputation: 980

I just came to this problem.

Ended using:

$(this).find('.is-active a').attr('id')

Upvotes: 0

Piet
Piet

Reputation: 2208

For Foundation 6 (current version is 6.2.1) you should use the following code to get the ID of the changed tab.

$('#myTabs').on('change.zf.tabs', function()
{
    var tabId = $('div[data-tabs-content="'+$(this).attr('id')+'"]').find('.tabs-panel.is-active').attr('id');
    alert(tabId);
}); 

Upvotes: 5

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

Try to add condition on tab id you want to load content when it's selected, e.g :

$('#myTabs').on('change.zf.tabs', function() {
   if ($(this).attr('id') == 'panel3')
   {
        //Load content here 
   }
}); 

Hope this helps.

Upvotes: 1

Dmitry  Dubovetsky
Dmitry Dubovetsky

Reputation: 494

You can use condition inside 'change.zf.tabs' event, like this:

$('#myTabs').on('change.zf.tabs', function() {

  if($('#panel3:visible').length){
    console.log('Tab 3 is now visible!');
  }

});

Upvotes: 14

Related Questions