Creating tabs using vanilla JavaScript

PHOTO EMBED

Fri Dec 10 2021 02:50:19 GMT+0000 (UTC)

Saved by @camtonguyen #javascript

const contents = document.querySelectorAll('.content > div');
const buttons = document.querySelectorAll('.tabBtns button');
buttons.forEach((button, i) => {
  button.addEventListener('click', () => {
    buttons.forEach(button => button.classList.remove('active'));
    contents.forEach(content => content.classList.add('hide'));
    button.classList.add('active');
    contents[i].classList.remove('hide');
  });
});
content_copyCOPY

https://stackoverflow.com/questions/66978996/is-there-a-better-way-of-creating-tabs-using-vanilla-javascript-than-this