Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

How to save, retrieve and remove data with JavaScript and HTML5

Posted on May 1, 2017August 4, 2020 by Habibie

Thanks to localStorage. Saving, retrieving and removing temporary data is very easy. To save data, we call this function: localStorage.setItem(); To retrieve data, we call localStorage.getItem(); and to remove it we call localStorage.removeItem();.

Below is simple source code to show you how this three functions work:

<!DOCTYPE html>
<html>
	<head>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<title>Experiments</title>
		<style>
			.myBox{
				border: 1px solid black;
				padding: 10px;
				margin-bottom: 10px;
			}
		</style>
	</head>
	<body>
		<script>
			//localStorage.removeItem(key);
		</script>
		<div class="myBox">
			<h1>Saving Data</h1>
			<input id="myInput" placeholder="Type anything...">
			<button onclick="saveData()">Save</button>
		</div>
		<div class="myBox">
			<h1>Saved Data</h1>
			<p id="savedData"></p>
		</div>
		<div class="myBox">
			<h1>Delete Data</h1>
			<button onclick="deleteData()">Delete</button>
		</div>
		<script>
			if(localStorage.getItem("myData") ===  null) document.getElementById("savedData").innerHTML = "No data is saved.";
			else document.getElementById("savedData").innerHTML = "Saved data: " + localStorage.getItem("myData");
			function saveData(){
				var newData = document.getElementById("myInput").value;
				if(newData != "") localStorage.setItem("myData", newData);
				location.reload();
			}
			function deleteData(){
				if(localStorage.getItem("myData") !== null) localStorage.removeItem("myData");
				location.reload();
			}
		</script>
	</body>
</html>

You can see live result of these codes here:

Post Views: 252
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
  • Advanced Blinking Marker Script to show objects position in your game canvas
  • Ciihuy Images Merger – Fast & Easy Online Image Combiner
© 2025 ThirteeNov | Powered by Superbs Personal Blog theme