0

I managed to reduce my jquery tabs to this current state where I define tabs ID in variables.

Question: How to modify this code to have more IDs?

For example:

var tabsId = '#tabs1', '#tabs2', ... ;
var containerId = '#tabs-container1', '#tabs-container2', ... 

JS:

var containerId = '#tabs-container';
var tabsId = '#tabs';

$(document).ready(function() {
    // Preload tab on page load
    if ($(tabsId + ' li.current a').length > 0) {
        loadTab($(tabsId + ' li.current a'));
    }

    $(tabsId + ' a').click(function() {
        if ($(this).parent().hasClass('current')) {
            return false;
        }

        $(tabsId + ' li.current').removeClass('current');
        $(this).parent().addClass('current');

        loadTab($(this));
        return false;
    });
});

function loadTab(tabObj) {
    if (!tabObj || !tabObj.length) {
        return;
    }
    $(containerId).addClass('loading');
    $(containerId).fadeOut('fast');

    $(containerId).load(tabObj.attr('href'), function() {
        $(containerId).removeClass('loading');
        $(containerId).fadeIn('fast');
    });
}

HTML:

<ul class="mytabs" id="tabs">
    <li class="current"><a href="./tabs/tab1.php">Tab 1</a></li>
    <li><a href="./tabs/tab2.php">Tab 2</a></li>
    <li><a href="./tabs/tab3.php">Tab 3</a></li>
</ul>

<div class="mytabs-container" id="tabs-container">
    Loading. Please Wait...
</div>
Ing. Michal Hudak
  • 5,338
  • 11
  • 60
  • 91

2 Answers2

0

You could use array's like

var tabIds = {'tab1','tab2'};

Then you can loop over the array elements. An alternative is to use the variables like you mentioned (comma separated) and split them. Then you can loop through them again. see this thread for an example: Split string with JavaScript

Community
  • 1
  • 1
Ruben Verschueren
  • 822
  • 13
  • 28
0

Actually, I don't se a single place in your code where you need your tabIds. It works exactly the same if you just remove it.

Unique instances of the .click() event will be added to each a element either way, and you don't have to specify them by ID´s. Here is an example with tabIds removed.

http://jsfiddle.net/c7RHs/

var containerId = '#tabs-container';

$(document).ready(function () {
    if ($('li.current a').length > 0) {
        loadTab($('li.current a'));
    }

    $('li a').click(function () {
        if ($(this).parent().hasClass('current')) {
            return false;
        }

        $('li.current').removeClass('current');
        $(this).parent().addClass('current');

        loadTab($(this));
        return false;
    });
});

function loadTab(tabObj) {
    if (!tabObj || !tabObj.length) {
        return;
    }
    $(containerId).addClass('loading');
    $(containerId).fadeOut('fast');

    $(containerId).load(tabObj.attr('href'), function () {
        $(containerId).removeClass('loading');
        $(containerId).fadeIn('fast');
    });
}

Otherwise, you could make an array of ID´s as suggested in another answer, but I don't think you need it.

Hope it helps!

Magnus Engdal
  • 5,446
  • 3
  • 31
  • 50