Home>
How do I change the backgroundImage of an image?
<div id='photo' style="background-image: url('img/image.png');"> </div><script> var photo= document.getElementById('photo')
photo.backgroundImage= url('img/image2.png')
</script>
-
Answer # 1
-
Answer # 2
Use to allow single or double quotes:
var photo= document.getElementById('photo'); photo.style.backgroundImage= `url('img/image2.png')`;
<div id='photo' style="background-image: url('img/image.png');"> </div>
Also, lines like this make it easy to insert variables:
var imagesUrl= ['img/image1.png','img/image2.png','img/image3.png']; var photo= document.getElementById('photo'); photo.style.backgroundImage= `url('${imagesUrl[1]}')`;
<div id='photo' style="background-image: url('img/image.png');"> </div>
why not just double quotes?
Grundy2022-02-02 21:45:07@Grundy: For perspective -what if the person decides to use doubles internally :)
UModeL2022-02-02 21:46:11
Related questions
- javascript : Block scrolling by clicking on the arrows
- javascript : Console gives error "Uncaught TypeError: Cannot read properties of null (reading 'style')" [duplicate]
- javascript : How to change the color of the scale in the video?
- javascript : html css + js how to position two charts (Chart_js) side by side rather than one below the other
- javascript : How to highlight one whole line in long HTML text
- javascript : How to emulate safari browser on windows
- javascript : Why won't the animation start?
- javascript : Dropdown menu on click
- javascript : Wrong handler in JS media requests
Use to allow single or double quotes:
Also, lines like this make it easy to insert variables:
why not just double quotes?
Grundy2022-02-02 21:45:07@Grundy: For perspective -what if the person decides to use doubles internally :)
UModeL2022-02-02 21:46:11