I have a problem with the for-each loop on a practice page.
There are 4 different popups on my page and all of them got a close button. So i wrote this lines four times into my html-code:
<div class="description-modal dm-1">
<i class="fa-solid fa-xmark close-m" id="close-m-1"></i>
<h3>Test</h3>
</div>
To activate them I gave them ID's from 1 to 4 and added the class "active" on click. To close them I tried to use this foreach loop:
const modals_all = document.querySelectorAll('.description-modal');
const close_buttons = document.querySelectorAll('.close-m')
modals_all.forEach(modal =>{
close_buttons.forEach(close_button =>{
close_button.addEventListener('click', ()=>{
if (modal.classList.contains('active')){
modal.classList.remove('active')
} else {
return
}
})
})
});
Unfortunatly it doesn't work and i have no idea why, cause the console doesn't show an error. What's the right way to solve this?