最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML local storage with button click - Stack Overflow

programmeradmin8浏览0评论

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:/

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/hhntg/

Share Improve this question asked Oct 22, 2013 at 20:23 user2892178user2892178 611 gold badge2 silver badges6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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/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 : )

From my observations. You have the function name as saveData where as you used save_data for the button

发布评论

评论列表(0)

  1. 暂无评论