I can't save to localstorage first input with save button. Trying to save multiple inputs to localStorage
I am new at this and I'm trying to save multiple inputs to localStorage with one save button. Its saved second one but not the first one and I can't quite get it to work. Can you please tell me what is the reason? Why doesn't it save the first one?
My inputs and save button:
<body onload="init()">
<input type="text" id="myTxt1" placeholder="Type here ..." ></br></br></br>
<input type="text" id="myTxt2" placeholder="Type here ..."></br></br>
<button onclick="onSavePressed()">Save</button>
</body>
My script:
function init(){
if (localStorage.first1Box){
document.getElementById("myTxt1").value = localStorage.first1Box;
}
}
function onSavePressed(){
localStorage.first1Box = document.getElementById("myTxt1").value;
alert("Saved Successfully!!!");
}
function init(){
if (localStorage.second2Box){
document.getElementById("myTxt2").value = localStorage.second2Box;
}
}
function onSavePressed(){
localStorage.second2Box = document.getElementById("myTxt2").value;
alert("Saved Successfully!!!");
}
Via Active questions tagged javascript - Stack Overflow https://ift.tt/WKON2ns
Comments
Post a Comment