I’m building an NFT gallery carousel and need help with image handling. When users upload a picture through a file input, I want to store it (preferably in localStorage or cookies without using backend languages) and display it only when that specific NFT card is active in the slider.
const backButton = document.querySelector("#back-btn");
const forwardButton = document.querySelector("#forward-btn");
let tokenTitle = document.querySelector("#title");
let tokenInfo = document.querySelector("#info");
let tokenValue = document.querySelector(".price");
let tokenDuration = document.querySelector("#duration");
let tokenArtist = document.querySelector("span");
let collection = [];
let activeToken;
let submitForm = document.querySelector(".form-submit");
let inputTitle = document.querySelector("#input-title");
let inputInfo = document.querySelector("#info-input");
let inputValue = document.querySelector("#value-input");
let inputDuration = document.querySelector("#duration-input");
let inputArtist = document.querySelector("#artist-input");
let chosenFile;
let imageToggle = document.getElementById("image-selector");
let imageDisplay = document.querySelector(".token-image");
let fileInput = document.createElement("input");
fileInput.setAttribute("type", "file");
fileInput.setAttribute("accept", "image/*");
class TokenCard {
constructor(id, title, info, value, duration, artist, picture) {
this.id = id;
this.title = title;
this.info = info;
this.value = value;
this.duration = duration;
this.artist = artist;
this.pictureData = picture;
}
}
let currentIndex = 0;
collection = [];
let addNewToken = function () {
chosenFile = fileInput.files[0];
let fileReader = new FileReader();
fileReader.addEventListener("load", function () {
imageDisplay.src = fileReader.result;
}, false);
if (chosenFile) {
fileReader.readAsDataURL(chosenFile);
}
let newToken = new TokenCard(
collection.length,
inputTitle.value,
inputInfo.value,
inputValue.value + "eth",
inputDuration.value,
inputArtist.value,
collection.length
);
collection.push(newToken);
inputTitle.value = "";
inputInfo.value = "";
inputValue.value = "";
inputDuration.value = "";
inputArtist.value = "";
};
let displayCurrentToken = function () {
activeToken = collection[currentIndex];
tokenTitle.textContent = activeToken.title;
tokenInfo.textContent = activeToken.info;
tokenValue.textContent = activeToken.value;
tokenDuration.textContent = activeToken.duration;
tokenArtist.textContent = activeToken.artist;
imageDisplay.src = activeToken.pictureData;
};
The main issue is properly linking uploaded images to their corresponding NFT cards and displaying the correct image when navigating through the carousel.