I have in localStorage array Storage {page-value: 'value'}
This value i need use in import. Usually i use import { examples } from "./Objects/Matematika1.js";
In this case everything works well. Array is imported correctly and logic works well. Issue is i need somehow change dynamicky import path. In menu i set IDs and onClick the ID is saved to localStorage
In my case i have value of object name in my localStorage for this demo value is matematika1. I need to use this value while importing.
import { examples } from "./Objects/"+selectedPage+".js";
This code is not working ofc. Google said It's not working like that...
Examples is just array of object. In /Object i have several files with same structured arrays and i need change the path depend on localStorage value.
import { examples } from "./Objects/Matematika1.js";
//Button logic for showing the result
function toggle(i) {
const div = document.querySelector(`#result_${i}`);
if (div.style.display !== "none") {
div.style.display = "none";
} else {
div.style.display = "block";
}
}
//main logic
const container = document.querySelector("#examples-container");
examples.forEach((ex, i) => {
const card = document.createElement("div");
card.classList.add("card");
const example = document.createElement("div");
example.classList.add("example");
example.innerHTML = ex.question;
card.appendChild(example);
const button = document.createElement("button");
button.classList.add("toggle");
button.innerHTML = "Toggle";
button.addEventListener("click", () => toggle(i)); // Here is the event listener
card.appendChild(button);
const result = document.createElement("div");
result.id = "result_" + i;
result.style.display = "none";
result.classList.add("result");
result.innerHTML = ex.answer;
card.appendChild(result);
container.appendChild(card);
});
//getting localStorage to string variable
const selectedPage = localStorage.getItem("page-value");
selectedPage.toString();
Is using localStorage good idea for this is or do you know something better? I practice programing few months so usually i do first idea what i get..