So I have a question. Right now I have a code that changes my image when I click on it, which is great. But I actually need it to function multiple times with different images. My conclusion: I need an array. However, I have yet to succeed in actually figuring out how to create a working one with this function.
I searched all over and managed to kind of gather a code that does something similar, but not quite (like changing images with an array but with an button) This JSfiddle is the one I'm talking about, with changing the image with the button https://jsfiddle.net/ffd7tmwy/1/
So I tried to edit it, but I failed. This is my attempt:
var change = document.getElementById('one');
var counter = 0;
var myPictures = [
"url(img/one_two.jpg"
];
function nextPic() {
counter += 1;
if (counter > myPictures.length -1) {
counter = 0;
}
change.src = myPictures[counter];
}
This is the one that is working but without the array or multiple pictures:
$(".one").on("click", function() {
$(this).css("background-image", "url(img/one_two.jpg)");
});
This is another one that did not work..
var backgroundImg = new Array();
backgroundImage[0] = "url/one_two.jpg";
];
$(function() {
$('.one').on('click', function(){
$('.one').css('background-image', 'url(' + backgroundImg[0] + ')');
console.log(backgroundImg);
});
I hope someone could help me and explain how I could fix it.
Thanks!