So I want to have some buttons opening modals with different content. But when I click them, the same content appears. How do I link them properly? (I have never asked in here so forgive me if I did any mistakes)
const modal = document.querySelector('.modal');
const overlay = document.querySelector('.overlay');
const btnCloseModal = document.querySelector('.close-modal');
const btnsOpenModal = document.querySelectorAll('.show-modal');
const openModal = function () {
modal.classList.remove('hidden');
overlay.classList.remove('hidden');
};
const closeModal = function () {
modal.classList.add('hidden');
overlay.classList.add('hidden');
};
for (let i = 0; i < btnsOpenModal.length; i++)
btnsOpenModal[i].addEventListener('click', openModal);
btnCloseModal.addEventListener('click', closeModal);
overlay.addEventListener('click', closeModal);
.hidden {
display: none;
}
<button class="show-modal">Show modal 1</button>
<button class="show-modal">Show modal 2</button>
<div class="modal hidden">
<button class="close-modal">×</button>
<p>Content1</p>
</div>
<div class="modal hidden">
<button class="close-modal">×</button>
<p>Content2</p>
</div>
<div class="overlay hidden"></div>