JSFiddle - React, Tailwind, and code Playground
HTML
<input type="text" id="nama" />
<input type="button" value="Simpan" onclick="simpan()" />
<input type="button" value="Tampil" onclick="tampil()" />
<input type="button" value="Hapus" onclick="hapus()" />
<div id="tampil"></div>
JavaScript
// fungsi simpan(), ketika tombol diklik maka string
// di dalam input akan tersimpan ke dalam storage browser
function simpan() {
var storage = localStorage.getItem('Text');
if(storage) storage = JSON.parse(storage);
else storage = [];
storage.push(document.getElementById('nama').value);
localStorage.setItem('Text',JSON.stringify(storage));
}
// tampil() akan menampilkan string yang tersimpan
// ke tag div yang ditentukan "hasil"
function tampil() {
var tampilNama = localStorage.getItem('Text');
if (tampilNama) {
x = document.getElementById('tampil');
x.innerHTML=tampilNama;
}
}
// fungsi untuk menghapus localstorage browser
function hapus() {
localStorage.removeItem('Text');
}