JSFiddle - React, Tailwind, and code Playground

by Stanimir Stoyanov

HTML

<input type="button" onClick="saveText();" value="Set" />
<input type="button" onClick="getText();" value="Get" />

JavaScript

function saveText() {
	if (typeof(Storage) !== "undefined") {
		localStorage.setItem("myData", "Hello, Rajesh!");
    alert('Data saved.');
  } else {
  	alert('Local storage not available');
	}
}

function getText() {
	if (typeof(Storage) !== "undefined" && typeof(localStorage.getItem('myData')) !== "undefined") {
    alert(localStorage.getItem('myData'));
  } else {
  	alert('Local storage not available or the data is not saved.');
	}
}