I have an input field named "mail". I want to add mailaddresses and ids into localstorage. The value for the mailaddress should come from the input field "mail" and the id should count up every time I add a mailaddress. At the moment there are static values for the vars "id" and "mail".
Can somebody give me a hint how I could do that?
Here is my code:
var id = 1;
var mail = "Mail";
$(".store").on("click", function(){
var mailaddress = localStorage.getItem("mailaddress");
var obj = [];
if(mailaddress){
obj= JSON.parse(mailaddress);
}
obj.push({"id": id, "mail":mail});
localStorage.setItem("mailaddress",JSON.stringify(obj));
});
$("#show").on("click", function(){
var item = JSON.parse(localStorage.getItem("mailaddress"));
alert(item);
console.log(item);
});
$('#reset').click(function(){
localStorage.clear();
});
<div id="storage">
<input type="text" id="mail" value=""/>
<button class="store">store</button>
<button id="reset">reset</button>
<button id="show">show</button></div>