how to remove dynamically loaded images in javascript

Posted by jesse on Stack Overflow See other posts from Stack Overflow or by jesse
Published on 2010-03-18T01:26:01Z Indexed on 2010/03/18 1:31 UTC
Read the original article Hit count: 361

Filed under:
|

I'm loading in 3 images (named 1.jpg, 2.jpg, 3jpg) dynamically to 3 divs called "div1", "div2" and "div3".

function loadImages() {

for (var i = 1; i < 3; i++ ) {
var img = document.createElement("img");
    img.src = "vegetables/"+i+".jpg";
    img.id = "a"+i+"";
    var divName = "div"+i+"";
    document.getElementById(divName).appendChild(img);
}

}

That works, but the removing part I can't seem to get to work..

function removeImages() {

for (var i = 1; i < 3; i++ ) {
    var oldImages = "a"+i+"";  
    var divName = "div"+i+"";
    document.getElementById(divName).removeChild(oldImages);
}

}

Thank you.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about removechild