How to change same images at multiple places using JS?

I want to know the solution that will help me change multiple same images using JS.

For example: There are 3 image tags below.

<img class="photo" src="myphoto.png">
<img class="photo" src="myphoto.png">
<img class="photo" src="myphoto.png">

And I want to change all the "myphoto.png" to "theirphoto.png" at once without change in every line.

>Solution :

You can get all of the elements with document.querySelectorAll and loop over them to change each src.

document.querySelectorAll('img.photo').forEach(img => img.src = 'theirphoto.png');

Leave a Reply