Reputation: 561
I'm searching for the way to display any image preloaded in a Javascript Image array in a chosen html object, because I couldn't find the way to do it so far: So, could you please help me write my function "affiche()", considering what follows:
for example, let's suppose we want to set a picture into a object called stand
now, the images have been loaded in cache as follows:
<script type= text/javascript>
// ...
var tbPix = new Array;
tbPix[1]= new Image();
tbPix[2]= new Image();
tbPix[3]= new Image();
tbPix[1].src = 'image1.jpg";
tbPix[2].src = 'image2.jpg";
tbPix[3].src = 'image3.jpg";
// ...
</script>
finally, we want to write the "affiche" function, with the following signature, so as to be able to call it like in the following example where we want to display the picture 'image2' from the Javascript cache into the 'stand' div:
<script type='text/javascript'>
// ...
affiche('stand',tbPix[2])
// ...
function affiche(divName,javaPic)
{
// give me the code, please!
}
// ...
</script>
Thank you very much by the way, dear 11501525 (well, I don't know your name) because tour answer was both fast and precisely what I needed. here's my function, knowing the same div object must contain no more than one image, so a new image has to replace the previous one, and not to be added:
function affiche(nomDiv, javaPic) //->void
{ // Place l'image javaPic dans l'objet nomDiv en remplacement de tout autre
var x = document.getElementById(nomDiv);
x.innerHTML = "";
document.getElementById(nomDiv).appendChild(javaPic);
}
Thanks a lot, once more.
Upvotes: 0
Views: 598
Reputation:
I think what you are searching for is appendChild:
divName.appendChild(javaPic);
Edit:
Sorry, i didn't saw that your first parameter was a string.
You need a html object in ordner to use appendChild.
If your object has the id stand, you can do this:
document.getElementById('stand').appendChild(javaPic);
Upvotes: 2