Wrap the images in a container element and attach a listener to it so you can use event delegation to catch the click events from the images as they "bubble up" the DOM (rather than adding listeners to all the images), and use a class.
When an image is clicked remove the "green" class from all the images, and add it to the image that was clicked.
const container = document.querySelector('#container'); const images = container.querySelectorAll('img'); container.addEventListener('click', handleClick, false); function handleClick(e) { if (e.target.nodeName === 'IMG') { images.forEach(image => image.classList.remove('green')); e.target.classList.add('green'); } }
img { border: 5px solid white; } .green { border: 5px solid green; }
<div id="container"> <img src="https://dummyimage.com/100x100/000/fff" /> <img src="https://dummyimage.com/100x100/555/fff" /> <img src="https://dummyimage.com/100x100/aaa/fff" /> </div>