I’m using jQuery
to try to change the background-image
each time I click a button. Currently my background is going through a for
loop each time the button is clicked, so the picture is going straight from the first picture to the last picture in my folder.
I know this is not what I’m aiming for but this is the only thing I’ve been able to get work without throwing errors:
$('#bg-change').on('click', function() {
for (let i = 1; i < 5; i++) {
$('#background').css('background-image', 'url("./img/blank-wall'+i+'.jpg"')
};
});
I’ve tried using an array as well as toggling classes but it can only get it to change between two images.
Please help!
2
Answers
You don’t need the
for
loop you can just use a counter variable and anif
statement and it should work.You can try this:
At first you should specify the number of images then define a variable (num) that each time when you click the button add 1 to it or if that was equal to the number of images simply redefine it as 1 then it can change the background by the value of
num
variable.