Search code examples
javascriptuser-input

How can I save user input?


I am making a JavaScript countdown clock extension, and I want the user to input a date, and have it save that date, but whenever the user opens the extension again, it doesn't save their previous input. is there any way to save the user input permanently? Thanks in advance!

var countDownDate = new Date(prompt("Enter Your Date")).getTime();

Solution

  • I'm just hooking you up with example from w3schools , you can read about localStorage api https://www.w3schools.com/html/html5_webstorage.asp

    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function clickCounter() {
        if(typeof(Storage) !== "undefined") {
            if (localStorage.clickcount) {
                localStorage.clickcount = Number(localStorage.clickcount)+1;
            } else {
                localStorage.clickcount = 1;
            }
            document.getElementById("result").innerHTML = "You have clicked the button " + localStorage.clickcount + " time(s).";
        } else {
            document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
        }
    }
    </script>
    </head>
    <body>
    <p><button onclick="clickCounter()" type="button">Click me!</button></p>
    <div id="result"></div>
    <p>Click the button to see the counter increase.</p>
    <p>Close the browser tab (or window), and try again, and the counter will continue to count (is not reset).</p>
    </body>
    </html>