Having trouble changing an image in Squarespace. I'm getting very close but I have not yet solved the problem.
I am trying to generate a random number, use that number to pick a photograph from my pictures array and replace the background of my gallery with that image.
My jQuery:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready( function() {
const index = randomMove(0, pictures.length),
picture = pictures[index];
// Gets the img tag under the imageWrapper class
const test = $('.imageWrapper > img');
// Gets the first index of the div (the image I want to manipulate)
const test1 = test[0];
// trying to change the picture
// I have tried attr('src', picture) as well and did not work
const test2 = $(test1).data("currentSrc", picture);
})
const randomMove = (mi, ma) => {
const min = Math.ceil(mi),
max = Math.floor(ma),
selection = Math.floor(Math.random() * (max - min) + min);
return selection;
};
const pictures = [
"https://i.imgur.com/bsG8hx7.jpg",
"https://i.imgur.com/Vuw28Lq.jpg",
"https://i.imgur.com/09Cfuks.jpg",
"https://i.imgur.com/3TRmorT.jpg",
"https://i.imgur.com/JElBKPO.jpg",
"https://i.imgur.com/eSTVz8D.jpg",
"https://i.imgur.com/3n1z9uc.jpg",
"https://i.imgur.com/aW5HWI5.jpg",
"https://i.imgur.com/5uEtErN.jpg",
"https://i.imgur.com/HMHehUy.jpg"
];
</script>
I believe the biggest challenge with this problem is that the HTML uses "data-src" and "data-image" in contrast to just a "src". This also may not be the correct way to solve this problem.
Here is my error log: https://image.prntscr.com/image/JaArgLTOT0WHY0oCkVdPCA.png
Here is the HTML code that showcases the aforementioned data-src and data-image attributes: https://image.prntscr.com/image/n1ME_XpXShifytEOfFVV8w.png