Suggested
Readings

document.addEventListener('DOMContentLoaded', function () { var tablists = document.querySelectorAll('[role="tablist"]'); tablists.forEach(function (tablist) { var tabs = tablist.querySelectorAll('[role="tab"]'); tabs.forEach(function (tab, i) { var isSelected = tab.getAttribute('aria-selected') === 'true'; tab.setAttribute('tabindex', isSelected ? '0' : '-1'); tab.addEventListener('keydown', function (e) { var newIndex = null; if (e.key === 'ArrowRight') newIndex = (i + 1) % tabs.length; if (e.key === 'ArrowLeft') newIndex = (i - 1 + tabs.length) % tabs.length; if (newIndex !== null) tabs[newIndex].focus(); }); tab.addEventListener('click', function () { tabs.forEach(function (t) { t.setAttribute('aria-selected', 'false'); t.setAttribute('tabindex', '-1'); }); tab.setAttribute('aria-selected', 'true'); tab.setAttribute('tabindex', '0'); }); }); }); });