$(document).ready(function () {
$(":button").click(function () {
var btn = $(":button").val();
if (btn == 'Favoritisieren')
$(":button").css("background-color", "red").prop('value', 'Favoritisiert');
var obj = {"Hed.1" : $("h1")};
var myJSON = JSON.stringify(obj);
localStorage.setItem('myJSON');
else
$(":button").css("background-color","blue").prop('value','Favoritisieren');
});
});
hey, I would like to save the filename and H1 value locally when clicking the button. These should also delete themselves later when the button is pressed again. Does anyone maybe have an idea where my error lies?
2
Answers
Use
localStorage.setItem(name, value)
to save a named item,var value = localStorage.getItem(name)
to read an item, andlocalStorage.removeItem(name)
to delete an item.Docs: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
Your code fixed to save the JSON, and to delete it later.
Assuming we have a button and h1 like this :
You can do it like this :
The syntax to write on localStorage :
The syntax for reading the localStorage item is as follows:
The syntax for removing the localStorage item is as follows:
The syntax for removing all the localStorage items is as follows: