HTML local storage with button click

12,254

Edited your jsfiddle to make things work. You just need to run that function when the button is clicked. Tested and verified working with localstorage through the inspector.

http://jsfiddle.net/hhntg/1/

var save_button = document.getElementById('Save')
save_button.onclick = saveData;

function saveData(){
  var input = document.getElementById("saveServer");
  localStorage.setItem("server", input.value);
  var storedValue = localStorage.getItem("server");
}

The only difference in the code here is that the function you wrote is attached to a click handler on the save element, you were nearly there : )

Share:
12,254

Related videos on Youtube

user2892178
Author by

user2892178

Updated on June 04, 2022

Comments

  • user2892178
    user2892178 almost 2 years

    Trying to store a bunch of text boxes once filled and button clicked to local storage. I've looked around and have only found this which I'm trying except im getting Uncaught ReferenceError: save_data is not defined. Any insight would be appreciated.

    <label for="serveri"> Server: </label> <input type='text' name="server" id="saveServer"/> <button onclick="save_data()" type="button" value="Save" id="Save">Save</button>

    <script>
    function saveData(){ var input = document.getElementById("saveServer"); 
    localStorage.setItem("server", input.value);
    var storedValue = localStorage.getItem("server"); } </script>
    

    If the above doesnt show my problem heres the full in jsfiddle:http://jsfiddle.net/hhntg/

  • user2892178
    user2892178 over 10 years
    awesome! I see it working for the server text box, is there a quick way to include the entire survey?
  • Jeff Escalante
    Jeff Escalante over 10 years
    Sure, you just need to get the rest of your inputs and save them using setItem like you did with the server input : )