I am using a ajax request to load data from the server. A php file presents an xml file. I get all the data succesfully but i cannot get the imgage on screen.
In the code below in the for ... loop i made some comments what i try to do. Can someone explain how i make the script load images wich i add dinamicly using javascript.
The valIMG is added at the bottom afther the button using appendChild.
function updatePlaneten(){
var valDiv, planets , valButton, textNode;
// Get xml files
planets = this.responseXML.getElementsByTagName("planeet");
// loop through the <planet> tags
for(var i=0;i<planets.length;i++){
(function(num){
valDiv = document.createElement("div");
valDiv.setAttribute("class", "divPresentPlanet" );
// I try to add an img element here.
// the variable imagename containts the path to the img: 'images/moon.jpg'
// appending it does add it to the div, tho the image isnt loaded
var valIMG = document.createElement("image");
var imagename = planets[num].getElementsByTagName("img")[0].childNodes[0].nodeValue;
valIMG.setAttribute("src", imagename );
valIMG.setAttribute("class", "IMGTAGPresentPlanet" );
// Get the id and the name from the xml info in current <planet> tag
var IDplanet = (planets[num].getElementsByTagName("id")[0].childNodes[0].nodeValue)-1;
var id = planets[num].getElementsByTagName("id")[0].childNodes[0].nodeValue + "<br>";
var name = planets[num].getElementsByTagName("name")[0].childNodes[0].nodeValue + "<br>";
var img = planets[num].getElementsByTagName("img")[0].childNodes[0].nodeValue + "<br>";
valDiv.innerHTML = id+"<br>"+name+"<br>"+ img +"<br>";
// Create button with a value and pass in this object for later reference use (valButton.object=this)
valButton = document.createElement("input");
// valButton.setAttribute("planeetID", planets[i].getElementsByTagName("id")[0].childNodes[0].nodeValue);
valButton.setAttribute("value", 'Meer info');
valButton.setAttribute("type", 'button');
valButton.id = num;
valButton.object = this;
// FIX: PASS showinfo TO AN ANONYMOUS FUNCTION CONTAINING THE OBJECT
valButton.addEventListener('click', function(){
showinfo(valButton, planets, IDplanet);
});
// Place the button on screen
valDiv.appendChild(valButton);
valDiv.appendChild(valIMG);
// place img tag on screen
valDiv.appendChild(valIMG);
document.getElementById("planetenID").appendChild(valDiv);
}(i));
}
}