Skip to content

Instantly share code, notes, and snippets.

@Jiert
Last active November 29, 2023 11:40
Show Gist options
  • Save Jiert/bbddb482c28f6c79cccc to your computer and use it in GitHub Desktop.
Save Jiert/bbddb482c28f6c79cccc to your computer and use it in GitHub Desktop.
Creating Tabs with Vanilla JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<title>Tabs</title>
<style>
.nav .active a { color: red; }
.tab-pane { display: none; }
.tab-pane.active { display: block; }
</style>
</head>
<body>
<div>
<!-- Tabs -->
<ul id="nav-tab" class="nav">
<li class="active"><a href="#home">Home</a></li>
<li><a href="#profile">Profile</a></li>
<li><a href="#messages">Messages</a></li>
<li><a href="#settings">Settings</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div class="tab-pane active" id="home">Home Panel</div>
<div class="tab-pane" id="profile">Profile Panel</div>
<div class="tab-pane" id="messages">Messages Panel</div>
<div class="tab-pane" id="settings">Settings Panel</div>
</div>
</div>
<footer>
<script type="text/javascript">
(function(){
function onTabClick(event){
var actives = document.querySelectorAll('.active');
// deactivate existing active tab and panel
for (var i=0; i < actives.length; i++){
actives[i].className = actives[i].className.replace('active', '');
}
// activate new tab and panel
event.target.parentElement.className += ' active';
document.getElementById(event.target.href.split('#')[1]).className += ' active';
}
var el = document.getElementById('nav-tab');
el.addEventListener('click', onTabClick, false);
})();
</script>
</footer>
</body>
</html>
@Zara603
Copy link

Zara603 commented May 8, 2020

There's few problems with this implementation, it's listening to any click on nav-tab so if user clicks on ul or li there's no href and event.target.href is undefined so you cant .split on it. Also instead of using split you could simply use getAttribute("href")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment