I have the following function:
function displayImage(n) {
var pic = document.createElement("img");
pic.setAttribute("src", h[n].imgUrl);
pic.setAttribute("width", "50");
l.appendChild(pic);
//l.innerHTML = "<img src=\"" + h[n].imgUrl + "\">";
};
The above would work fine if it wasn't registered to a button (each time one clicks on a button, a relevant image gets loaded. As it is now, it keeps adding new images, each time I click, which is understandable. What I want is it to clear the content on the div and replace it with the new image. The commented out version in the code above worked fine but it did not give me possibilities to set attributes, which I have to do (I know I could set attributes in css, but in this instance I can't). How would I change it so that each time I run the function, the old img gets replaced with a new one? Thank you
<div>where you want to insert this image, and the<img>you want to replace.