mardi 23 juillet 2019

Random image with javascript doesnt want to give the src

I want to show a random picture with every refresh but I don't know why my code is not working... probably because I'm bad at javascript, haha.

I tried "3" instead of "imgs.length", but it's still not working - I too don't really want to give an exact number, because the image count may vary.. only for my example I use three images.

var imgs = ['img1','img2','img3'];

function getRandomImage(){
 var rnd = Math.floor(Math.random()*imgs.length);
 document.getElementById('pr_randImage').src = imgs[rnd];
}   
</script>   

<img id="pr_randImage">

Quellcode is just not showing any src at all for the img. What am I doing wrong? I'm thankful for every help.




Aucun commentaire:

Enregistrer un commentaire