I've been trying to get a random image to show up on load. This is the code I'm using:
<head>
<script type="text/javascript">
ImageArray = new Array();
image[0] = 'goat1.jpg';
image[1] = 'kitchen4.jpg';
image[2] = 'pig1.jpg';
image[3] = 'site1.jpg';
image[4] = 'site2.jpg';
image[5] = 'site3.jpg';
image[6] = 'site4.jpg';
image[7] = 'site5.jpg';
image[8] = 'site6.jpg';
image[9] = 'site7.jpg';
image[10] = 'site8.jpg';
function getRandomImage() {
var num = Math.floor( Math.random() * 11);
var img = ImageArray[num];
document.getElementById("randImage").textContent = ('<img src="' + 'images/random/' + img + '" width="250px">')
}
</script>
</head>
<body onload="getRandomImage()">
<div id="randImage"></div>
</body>
I'm new to javascript, and cobbled this together from snippets I found on line.
The problem with this code is that it shows <img src="images/random/undefined" width="250px">
, instead of an image.