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

เมื่อคลิกปุ่มสร้าง -

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