Json local storage

Hi, i´m trying to add local storage with typescript. But I´m struggling to understand. I have to use json.parse but I´m not sure on the rest. I would appreciate some help

voorbeeld2(); 

const json = {"name"};
opslag = JSON.parse(json);

function voorbeeld2(): void {
    const naamElement: HTMLInputElement = document.querySelector("#uitjenaam") as HTMLInputElement;
    const knopElement: HTMLButtonElement = document.querySelector("#uitjebevestigen") as HTMLButtonElement;

    // voorbeeld 2
    knopElement.addEventListener("click", () => {
        if (naamElement.value) {
        }
        else {
            alert("Voer een uitje in.");
           // saveLocalStorage("Opgeslagen uitjes", naamElement.value);
        }
    });
}

JSON.parse is used to parse JSON string into an object to work with it.
JSON.stringify is used to stringify an object into a string that can be stored in the localStorage since it only accepts strings.

Example:

const jsonString = JSON.stringify({ "name": "John" });  // Create a JSON string
const opslag = JSON.parse(jsonString);  // Parse the JSON string back to an object
console.log(opslag.name);  // Outputs: "John"

However, I think your code isn’t complete as I can’t see the saveLocalStorage function declaration, but this MDN documentation will be very helpful if you read it, particularly this section Setting values in storage.

1 Like