-
Notifications
You must be signed in to change notification settings - Fork 0
/
d.java
53 lines (47 loc) · 1.86 KB
/
d.java
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
document.addEventListener('DOMContentLoaded', function() {
// Retrieve goals from localStorage when the DOM is fully loaded
var savedGoals = JSON.parse(localStorage.getItem('goals')) || [];
// Display saved goals on the page
var goalList = document.getElementById('goal-list');
savedGoals.forEach(function(goalText) {
addGoalToDOM(goalText);
});
// Function to add a goal item to the DOM
function addGoalToDOM(goalText) {
var li = document.createElement('li');
li.textContent = goalText;
// Create a delete button (small "x" character)
var deleteButton = document.createElement('button');
deleteButton.innerHTML = '×'; // Unicode for "x" character
deleteButton.classList.add('delete-button');
deleteButton.addEventListener('click', function() {
// Remove the goal from the DOM
goalList.removeChild(li);
// Remove the goal from localStorage
var index = savedGoals.indexOf(goalText);
if (index !== -1) {
savedGoals.splice(index, 1);
localStorage.setItem('goals', JSON.stringify(savedGoals));
}
});
// Append the delete button to the goal item
li.appendChild(deleteButton);
// Append the goal item to the goal list
goalList.appendChild(li);
}
// Add event listener to the form to save new goals
document.getElementById('goal-form').addEventListener('submit', function(event) {
event.preventDefault();
var goalInput = document.getElementById('goal');
var goalText = goalInput.value.trim();
if (goalText !== '') {
// Add new goal to the DOM
addGoalToDOM(goalText);
// Save the new goal to localStorage
savedGoals.push(goalText);
localStorage.setItem('goals', JSON.stringify(savedGoals));
// Clear the input field
goalInput.value = '';
}
});
});