Vanilla JavaScript can't add image with set src (image not loaded)

Viewed 205

I search to load an image with JS, I know his path, and I would like the source of the image to be added dynamically.

I have no error in the console and the image is displayed after removing the display to none but remains a browser icon.

The image source is being updated. But not displayed. If I right clic on browser icon, and I reload the image, it's works.

enter image description here

My code:

<div id="result" style="display: none;">
    <img src="" alt="" id="myImage">
</div>

<script>
var path = '/assets/image.png';
var result = document.querySelector('#result');
var image = document.querySelector('img#myImage');
image.src = path;
result.style = 'display: block;';
</script>
0 Answers
Related