I have this simple page set up which will add or remove a hidden class on an element when either button is clicked. The code works fine as is, but I know that it's very inefficient. I plan on adding more buttons and don't want to make a new function and event listener for each one. Rather than having a separate function for each button I would like to make 1 function which can differentiate which button was pressed and add the hidden class based on that. Any ideas?
<style>
.hidden {
display: none;
}
</style>
<body>
<header>
<button class="nav-link link-home">Home</button>
<button class="nav-link link-about">About</button>
</header>
<main>
<h1 class="main-content home-page hidden">This is the Home Page</h1>
<h1 class="main-content about-page hidden">This is the About Page</h1>
</main>
<script>
const navLink = document.querySelectorAll('.nav-link');
const linkHome = document.querySelector('.link-home');
const linkAbout = document.querySelector('.link-about');
const mainContent = document.querySelectorAll('.main-content');
const homePage = document.querySelector('.home-page');
const aboutPage = document.querySelector('.about-page');
const openHome = () => {
homePage.classList.remove('hidden');
aboutPage.classList.add('hidden');
};
linkHome.addEventListener('click', openHome);
const openAbout = () => {
aboutPage.classList.remove('hidden');
homePage.classList.add('hidden');
};
linkAbout.addEventListener('click', openAbout);
</script>
</body>