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 do I get an image stored in a javascript variable, to display in an HTML code?

How do I get an image stored in a javascript variable, to display in an HTML code?

Say for instance, my image is stored in a variable called flowers as seen in the below javascript code:

<script>
     var flowers = "myFlowers.gif";
</script>

In the following HTML code, I reference the flowers variable in my img src in order to display the image but cant seem to get this to work.

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

<img src= flowers  class="card-img-top" id="flowers">

Please help, What am I doing wrong?

>Solution :

You can’t insert it directly like that. Instead you set the attribute src through javascript.

let flowers = "https://picsum.photos/200/300";
document.querySelector('#flowers').src = flowers;
<img  class="card-img-top" id="flowers">
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