xxxxxxxxxx
<script>
//aks solutions:
// Set Item
let data = [
{"name":"John", "age":30, "city":"New York"},
{"name":"akash", "age":24, "city":"Bangladesh"}
];
// Get Item
localStorage.setItem("mydata", JSON.stringify(data));
let getData = localStorage.getItem("mydata");
console.log(JSON.parse(getData))
</script>
localStorage setItems and getItems
xxxxxxxxxx
//Set item
localStorage.setItem('myCat', 'Tom');
//Get item
var cat = localStorage.getItem("myCat");
//Remove item
localStorage.removeItem("lastname");
//Remove all items
localStorage.clear();
xxxxxxxxxx
localStorage.setItem('myCat', 'Tom');
var cat = localStorage.getItem('myCat');
localStorage.removeItem('myCat');
// Clear all items
localStorage.clear();
xxxxxxxxxx
const person = {
name: "Obaseki Nosa",
location: "Lagos",
}
window.localStorage.setItem('user', JSON.stringify(person));
xxxxxxxxxx
// Store
localStorage.lastname = "Smith";
// Retrieve
document.getElementById("result").innerHTML = localStorage.lastname;
// Remove
localStorage.removeItem("lastname");
xxxxxxxxxx
<!-- Save to local storage with a key and item -->
localStorage.setItem('stringKey', item)
<!-- localStorage.getItem will ONLY return a string regardless of what type was saved -->
let string = localStorage.getItem('stringKey')
<!-- JSON.parse will get return the item as it's original type -->
JSON.parse(localStorage.getItem('stringKey'))
xxxxxxxxxx
<!DOCTYPE html>
<html>
<head>
<title>javascript</title>
</head>
<body>
<input id="textbox" type="text" /><br><br>
<button id="btn">Save</button>
<br><br>
<script type="text/javascript">
var textbox = document.getElementById("textbox");
var button = document.getElementById("btn");
button.addEventListener("click", function(){
localStorage.name = textbox.value;
});
document.write(localStorage.name);
</script>
</body>
</html>