hmn/public/js/tabs.js

25 lines
783 B
JavaScript
Raw Normal View History

2024-06-24 18:36:50 +00:00
function initTabs(container, initialTab = null) {
const buttons = Array.from(container.querySelectorAll("[data-tab-button]"));
const tabs = Array.from(container.querySelectorAll("[data-tab]"));
2024-06-24 18:36:50 +00:00
if (!initialTab) {
initialTab = tabs[0].getAttribute("data-tab");
}
2024-06-24 18:36:50 +00:00
function switchTo(name) {
for (const tab of tabs) {
tab.hidden = tab.getAttribute("data-tab") !== name;
}
for (const button of buttons) {
button.classList.toggle("tab-button-active", button.getAttribute("data-tab-button") === name);
}
}
2024-06-24 18:36:50 +00:00
switchTo(initialTab);
2024-06-24 18:36:50 +00:00
for (const button of buttons) {
button.addEventListener("click", () => {
switchTo(button.getAttribute("data-tab-button"));
});
}
2021-11-25 03:59:51 +00:00
}