document.addEventListener('click', toggleImage)
function toggleImage ({target}) {
const buttonToggle = target.closest('[show-img-alt]')
if (buttonToggle === null) return
const alt = buttonToggle.getAttribute('show-img-alt')
const allImages = document.querySelectorAll('img')
for (const img of allImages) {
if (img.alt !== alt) img.style.display = 'none'
else img.style.display = ''
}
}
Или
function toggleImages (alt) {
const allImg = document.querySelectorAll('img')
for (const img of allImg) {
if (img.alt !== alt) img.style.display = 'none'
else img.style.display = ''
}
}
Или
function toggleImages (alt) {
document.querySelectorAll('img').forEach(img => img.style.display = 'none')
document.querySelectorAll(`img[alt="${alt}"]`).forEach(img => img.style.display = '')
}