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 can I write the content of a variable in image src while using document.getElementById?

I have the following code

document.write("<img src=./", i, ".png width='100px' height='100px'>");
document.write("<img src=./", x, ".png width='100px' height='100px'>");
document.write("<img src=./", y, ".png width='100px' height='100px'>");`

and I want to use the src in getElementById(myImg).InnerHTML.

I tried this

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

document.getElementById("myImg").innerHTML.src = "./", i, ".png width='100px' height='100px'>";

But It’s not working

What is the proper way to write it?

>Solution :

Change the src, not InnerHTML.src. You can see the answer in this question.
and you need to set the width height with style

document.getElementById("myImg").src=`./${i}.png`;
document.getElementById("myImg").style.width="100px";
document.getElementById("myImg").style.height="100px";
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