简体   繁体   中英

Saving user input to local storage

I have the code to save user input for a single input box here and i would like to have it save multiple user inputs. I have asked people and they have told me to do things i dont fully understand, could someone rewrite this for me to where i can have multiple ids? Id rather not right this 20+ times for each individual id. Thanks!

var storedItem = localStorage.getItem("name")

    function save(){
        var Item = document.getElementById("name").value;
        localStorage.setItem("name", Item);
    }
    function get(){
        document.getElementById('name').value = localStorage.getItem('name');

        

    }

You can just pass the id of the field to the function and save/get it that way

function save(id) {
    var Item = document.getElementById(id).value;
    localStorage.setItem(id, Item);
}

function get(id) {
    document.getElementById(id).value = localStorage.getItem(id);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM