-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.html
More file actions
101 lines (84 loc) · 2.46 KB
/
Copy pathsample.html
File metadata and controls
101 lines (84 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.input-container {
margin-bottom: 20px;
}
.data-list {
margin-top: 20px;
}
.data-item {
margin: 10px 0;
}
</style>
<title>Simple Local Storage Example</title>
</head>
<body>
<h1>Simple Local Storage Database</h1>
<div class="input-container">
<label for="dataInput">Enter Data:</label><br>
<input type="text" id="dataInput" placeholder="Enter some data">
<button onclick="saveData()">Save Data</button>
</div>
<div>
<h2>Stored Data:</h2>
<div id="dataList" class="data-list"></div>
</div>
<script>
// Function to save data to localStorage
function saveData() {
const input = document.getElementById("dataInput");
const data = input.value;
if (data) {
// Get existing data from localStorage or initialize an empty array
let storedData = JSON.parse(localStorage.getItem("myData")) || [];
// Add new data
storedData.push(data);
// Save updated data back to localStorage
localStorage.setItem("myData", JSON.stringify(storedData));
// Clear the input field
input.value = "";
// Refresh the displayed data
displayData();
} else {
alert("Please enter some data.");
}
}
// Function to display data from localStorage
function displayData() {
const dataList = document.getElementById("dataList");
const storedData = JSON.parse(localStorage.getItem("myData")) || [];
// Clear existing content
dataList.innerHTML = "";
// Display each stored item
storedData.forEach((item, index) => {
const dataItem = document.createElement("div");
dataItem.className = "data-item";
dataItem.innerHTML = `
${index + 1}. ${item}
<button onclick="deleteData(${index})">Delete</button>
`;
dataList.appendChild(dataItem);
});
}
// Function to delete data from localStorage
function deleteData(index) {
let storedData = JSON.parse(localStorage.getItem("myData")) || [];
// Remove the selected item
storedData.splice(index, 1);
// Save updated data back to localStorage
localStorage.setItem("myData", JSON.stringify(storedData));
// Refresh the displayed data
displayData();
}
// Display stored data on page load
document.addEventListener("DOMContentLoaded", displayData);
</script>
</body>
</html>