Back to feed
ForumSpark Support Themes & Extensions Join

Tabbed Categories

Cory · 7 months ago · 5 replies · 70

Preview

The first category is shown by default, and you can tab through the other categories with the buttons above the category.

Admin CP → Themes → Board Templates → Below the board

<script>
if (document.querySelector('div.forumlist')) {
    document.querySelector('div.forumlist').insertAdjacentHTML('beforebegin', `<div id="cat-tabs" style="margin: 1.5rem 0"></div>`);
}

const category = document.querySelectorAll('div[id^="fourm-"]');
const catTitle = document.querySelectorAll('div.category h2 a');

for (let i = 1; i < category.length; i++) {
    category[i].style.display = 'none';
}

for (const i of catTitle) {
    const tabButton = document.createElement('button');
    tabButton.type = 'button';
    tabButton.id = 'tab-' + i.parentNode.parentNode.parentNode.id.split('fourm-')[1];
    tabButton.className = 'btn btn-secondary btn-sm';
    tabButton.style.marginRight = '5px';
    tabButton.innerText = i.innerText;

if (document.querySelector('#cat-tabs')) {
    document.querySelector('#cat-tabs').appendChild(tabButton);
}
}

const tabButtons = document.querySelectorAll('#cat-tabs button');

for (const element of tabButtons) {
    element.onclick = function() {
        const forumID = 'fourm-' + this.id.split('tab-')[1];

    for (const x of category) {
        x.style.display = 'none';
    }

    document.querySelector('#' + forumID).style.display = 'block';
};
}
</script>
5 replies
Empire · 7 months ago

Noce :star_struck:

I modified the original forum category tab script slightly so it works better for my setup.

Changes I made:
• Added an "All Boards" button so you can show every category again
• Centred the tab buttons
• Added active tab highlighting
• Cleaned up some of the JavaScript
• Improved button spacing and layout

Feel free to use or modify it.

<style>
#cat-tabs{
    margin: 1.5rem 0;
    text-align:center;
}

#cat-tabs button{
    margin:4px;
    padding:6px 12px;
    border-radius:6px;
    cursor:pointer;
}

#cat-tabs button.active{
    background:#0d6efd;
    color:#fff;
}
</style>

<script>
const forumList = document.querySelector('div.forumlist');

if (forumList) {
    forumList.insertAdjacentHTML('beforebegin','<div id="cat-tabs"></div>');
}

const categories = document.querySelectorAll('div[id^="fourm-"]');
const titles = document.querySelectorAll('div.category h2 a');
const tabsContainer = document.querySelector('#cat-tabs');

/* Create ALL button */
const allBtn = document.createElement('button');
allBtn.textContent = "All Boards";
allBtn.className = "btn btn-primary btn-sm active";
tabsContainer.appendChild(allBtn);

/* Show all boards when clicked */
allBtn.onclick = function(){

categories.forEach(cat => {
    cat.style.display = "block";
});

document.querySelectorAll('#cat-tabs button').forEach(b => b.classList.remove("active"));
this.classList.add("active");
};

/* Create buttons for each category */
titles.forEach(title => {

const forumID = title.closest('div[id^="fourm-"]').id.replace('fourm-','');

const btn = document.createElement('button');
btn.textContent = title.textContent;
btn.className = "btn btn-secondary btn-sm";
btn.id = "tab-" + forumID;

tabsContainer.appendChild(btn);

btn.onclick = function(){

    categories.forEach(cat => {
        cat.style.display = "none";
    });

    document.querySelector('#fourm-' + forumID).style.display = "block";

    document.querySelectorAll('#cat-tabs button').forEach(b => b.classList.remove("active"));
    this.classList.add("active");

};

});
</script>
Cory OP · 7 months ago

Nice job, empire! It looks a lot better than my version!

Korok · 7 months ago

This is awesome! Is there a way to make the boards themselves tabbed rather than the categories? If so, how would I go about doing that? Would it be just a simple tweak of this code?

Empire · 7 months ago
Korok said:

This is awesome! Is there a way to make the boards themselves tabbed rather than the categories? If so, how would I go about doing that? Would it be just a simple tweak of this code?

Yes, if you want the individual boards/forums to become tabs instead of the category blocks, it is basically the same idea, but you need to target the board elements rather than the category wrappers.

Instead of this:

const categories = document.querySelectorAll('div[id^="fourm-"]');
const titles = document.querySelectorAll('div.category h2 a');

you’d do something more like:

const boards = document.querySelectorAll('.YOUR-BOARD-SELECTOR');
const tabsContainer = document.querySelector('#cat-tabs');

Here is a clean example. You will need to swap .board-item and .board-title for the actual selectors on your page.

<style>
#board-tabs{
    margin: 1.5rem 0;
    text-align: center;
}

#board-tabs button{
    margin: 4px;
    padding: 6px 12px;
    border-radius: 6px;
    cursor: pointer;
    border: 1px solid #ccc;
    background: #f5f5f5;
}

#board-tabs button.active{
    background: #0d6efd;
    color: #fff;
    border-color: #0d6efd;
}
</style>

<script>
const forumList = document.querySelector('div.forumlist');

if (forumList) {
    forumList.insertAdjacentHTML('beforebegin', '<div id="board-tabs"></div>');
}

const tabsContainer = document.querySelector('#board-tabs');

/* CHANGE THESE SELECTORS TO MATCH YOUR ACTUAL BOARD ELEMENTS */
const boards = document.querySelectorAll('.board-item');

if (tabsContainer && boards.length) {

/* All Boards button */
const allBtn = document.createElement('button');
allBtn.textContent = "All Boards";
allBtn.className = "active";
tabsContainer.appendChild(allBtn);

allBtn.onclick = function () {
    boards.forEach(board => {
        board.style.display = "";
    });

    document.querySelectorAll('#board-tabs button').forEach(b => b.classList.remove('active'));
    this.classList.add('active');
};

boards.forEach((board, index) => {
    const titleEl = board.querySelector('.board-title');
    if (!titleEl) return;

    const boardID = 'board-tab-' + index;
    const btn = document.createElement('button');
    btn.textContent = titleEl.textContent.trim();
    btn.id = boardID;

    tabsContainer.appendChild(btn);

    btn.onclick = function () {
        boards.forEach(b => {
            b.style.display = "none";
        });

        board.style.display = "";

        document.querySelectorAll('#board-tabs button').forEach(b => b.classList.remove('active'));
        this.classList.add('active');
    };
});
}
</script>
Korok · 7 months ago

@empire That's awesome! Thank you for the instructions!

Create a free account to join the conversation.
Sign up free
ForumSpark © 2023 - 2026 · ForumSpark.net · Terms