Skip to content

Instantly share code, notes, and snippets.

@mankutila
Last active November 9, 2017 10:56
Show Gist options
  • Save mankutila/c496b77387e6337f370aba5885dbb35c to your computer and use it in GitHub Desktop.
Save mankutila/c496b77387e6337f370aba5885dbb35c to your computer and use it in GitHub Desktop.
Tabs on pure JS
var tabLinks = document.querySelectorAll('ul.tabs li a');
tabLinks[0].setAttribute('class', 'tab--active');
document.querySelectorAll('section.tab-content')[0].style.display = 'block';
for (var i = 0; i < tabLinks.length; i++) {
tabLinks[i].onclick = function() {
var target = this.getAttribute('href').replace('#', '');
var sections = document.querySelectorAll('section.tab-content');
for(var j=0; j < sections.length; j++) {
sections[j].style.display = 'none';
}
document.getElementById(target).style.display = 'block';
for(var k=0; k < tabLinks.length; k++) {
tabLinks[k].removeAttribute('class');
}
this.setAttribute('class', 'tab--active');
return false;
}
};
/*css*/
.tab-content {
display:none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment