im trying to store an image in HTML that comes from a url source in a variable using JavaScript. Could someone show me what code is required? i have started it off…
JavaScript
x
6
1
var productImage = document.getElementById("productImg").//??;
2
3
4
<img id="productImg"
5
src="https://i5.walmartimages.com/asr/1735db1c-d84f-417a-b871-27b63ee2b2e6_1.9a18f15c0e0fa321d0c5d073875b9738.jpeg?odnWidth=undefined&odnHeight=undefined&odnBg=ffffff" >
6
Advertisement
Answer
You can use the src
property.
JavaScript
1
2
1
var productImage = document.getElementById("productImg").src
2
You can also use the src
property to set a new URL for the image if you want to replace it.
JavaScript
1
3
1
var productImage = document.getElementById("productImg");
2
productImage.src = URL
3