appendChild / removeChild – swapping images on the fly

I have an image inside of a div that I’d like to swap every time the function is triggered. I was able to attach first image through this:

this.titlecontainer.appendChild(document.createElement('img')).src = 'images/picture' + padded_index + '-t.png';

This indeed does add another image on each function iteration but as the code suggests, it actually APPENDS additional images. Instead, I’d like to SWAP the existing image for a new one. My guess is to remove what’s currently loaded inside the div prior to appending another one. What would the line preceding the above have to look like to accomplish removing of the content?

Many thanks

Instead of appending a new child element, just change the src of the existing img.

document.getElementById('myimg').src = 'images/picture' + padded_index + '-t.png';

thanks Clark