เมธอด HTML DOM Storage removeItem() ใช้สำหรับลบรายการอ็อบเจ็กต์หน่วยเก็บข้อมูลโดยส่งชื่อคีย์ที่กำหนด
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์สำหรับเมธอด Storage removeItem() -
localStorage.removeItem(keyname);
หรือ
sessionStorage.removeItem(keyname);
ในที่นี้ ชื่อคีย์เป็นประเภทสตริงและแสดงถึงชื่อของรายการที่จะลบ
ตัวอย่าง
ให้เราดูตัวอย่างสำหรับเมธอด Storage removeItem() -
<!DOCTYPE html>
<html>
<body>
<h1 style="text-align:center">Storage removeItem() method example</h1>
<p>Delete the localstorage item by clicking the below button</p>
<button onclick="itemDelete>REMOVE</button>
<p>Display the localstorage item by clicking the below button</p>
<button onclick="itemShow>DISPLAY</button>
<p id="Sample"></p>
<script>
localStorage.setItem("TEXT1","HELLO WORLD");
function itemDelete() {
localStorage.removeItem("TEXT1");
itemShow();
}
function itemShow() {
var x = localStorage.getItem("TEXT1");
document.getElementById("Sample").innerHTML ="The 'TEXT1' key value is "+x;
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

เมื่อคลิกปุ่ม DISPLAY -

เมื่อคลิกที่ปุ่ม REMOVE -
