I have a variable with an empty array.
var imageData = [];
var myIndex = 0;
onClick of an element I add items to the array.
["item_1", "item_2", "item_3", "item_4", "item_5"]
This array then gets added to a div that has a background image.
$('.final_div').css('background-image', 'url(https://url/path/images/' + imageData[myIndex] + '.png)');
So i've just added a prev/next button. I added:
myIndex = myIndex +1; to the next button and then myIndex = myIndex -1; to the prev button. However, after so many iterations I get back nothing, as in myIndex is undefined anyone know what i'm doing wrong?
I've seen this question a few times but not sure what i've missed. Thanks!
EDIT: Sorry! Here's more full view of the code.
My array: ["item_1", "item_2", "item_3", "item_4", "item_5"]
My next onClick:
$('.btn-right').on('click', function(){
myIndex = myIndex + 1;
$('.final_div').css('background-image', 'url(https://url/path/images/' + imageData[myIndex] + '.png)');
});
My prev onClick:
$('.btn-left').on('click', function(){
myIndex = myIndex - 1;
$('.final_div').css('background-image', 'url(https://url/path/images/' + imageData[myIndex] + '.png)');
});
onclickcode...myIndex++andmyIndex--?