images = document.getElementsByTagName('img'); for (img of images) { img.addEventListener('click', function() { if (img.classList.contains("fullscreen")) { img.classList.remove('fullscreen'); } else { img.classList.add('fullscreen'); } }); }