I have four divs, and I want them to show random images. I got this code from SO and it works:
<script type="text/javascript">
function get_image(div_id){
var imgCount = 3;
var dir = 'images/';
var randomCount = Math.round(Math.random() * (imgCount - 1)) + 1;
var images = new Array
images[1] = "1.jpg",
images[2] = "2.jpg",
images[3] = "3.jpg",
document.getElementById(div_id).style.backgroundImage = "url(" + dir + images[randomCount] + ")";
console.log("div_id = " + div_id)
}
</script>
It is called from the divs like this:
<div class="image_holder" id="left_top">
<script id="lt_innerscript">
var div_id = document.getElementById("lt_innerscript").parentElement.id;
var myVar = setInterval(function() { get_image(div_id); }, 800);
</script>
</div>
The problem is, only the last one is showing an image, all the others are blank. Is this possible, and what am I doing wrong?
There is some css to style the divs, they live in 2 separate divs, with ids "left" and "right."
#left{
float: left;
background: blue;
width: 300px;
height: 100%;
color: #005CB9;
}
and
.image_holder {
position: relative;
width: 300px;
height: 256px;
}