I am working with an images array that contains three jpg files. I am trying to set the background image of a class called pic using images from the array.
The issue I'm facing is that the images are stored in an images folder with the URL images/
. I need to add images/
to the line.
document.getElementById('content').style.backgroundImage = 'url('+images[index]+')';
Can anyone help me with this?
var images = ['bus.jpg','car.jpg','scooter.jpg' ];
var index = 0;
function buildImage() {
document.getElementById('content').style.backgroundImage = 'url('+images[index]+')';
}
function changeImage() {
index++;
if (index >= images.length) index = 0;
document.getElementById('content').style.backgroundImage = 'url('+ images[index] + ')';
}
<div class="pic" id="content" >