Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading