-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (69 loc) · 2.56 KB
/
script.js
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
const checkBoxList = document.querySelectorAll('.custom-checkbox');
const inputFields = document.querySelectorAll('.goal-input');
const errorLabel = document.querySelector('.error-label');
const progressLabel=document.querySelector('.progress-label')
const progressValue = document.querySelector('.progress-value');
const allGoals = JSON.parse(localStorage.getItem('allGoals')) || {
first:{
name:'',
completed:false
},
second:{
name:'',
completed:false
},
third:{
name:'',
completed:false
},
};
const quote=[
'Raise the bar by completing your goals!',
'Well Began is half done!',
'Just a step away,keep going!',
"Woha! you just completed all goals,It's time for chill 🎉"
]
let goalCount = Object.values(allGoals).filter((goal) => goal.completed).length;
progressValue.firstElementChild.innerText=`${goalCount}/3 Completed`;
progressLabel.innerText=quote[goalCount]
checkBoxList.forEach((checkbox) => {
checkbox.addEventListener('click', () => {
const allGoalsAdded = [...inputFields].every((input) => input.value.trim());
if (allGoalsAdded) {
checkbox.parentElement.classList.toggle("completed");
const inputId = checkbox.nextElementSibling.id;
allGoals[inputId] = {
...allGoals[inputId],
completed: !allGoals[inputId]?.completed,
};
goalCount = Object.values(allGoals).filter((goal) => goal.completed).length;
progressValue.style.width = `${(goalCount / Object.keys(allGoals).length) * 100}%`;
progressValue.firstElementChild.innerText=`${goalCount}/3 Completed`;
progressLabel.innerText=quote[goalCount];
localStorage.setItem('allGoals', JSON.stringify(allGoals));
} else {
errorLabel.style.display = 'block';
}
});
});
inputFields.forEach((input) => {
const goal = allGoals[input.id] || { name: '', completed: false };
input.value = goal.name;
if (goal.completed) {
input.parentElement.classList.add('completed');
}
input.addEventListener('focus', () => {
errorLabel.style.display = 'none';
});
input.addEventListener('input', (e) => {
if (allGoals[input.id].completed) {
e.target.value=allGoals[input.id].name
return
}
allGoals[input.id] = {
name: input.value.trim(),
completed: allGoals[input.id]?.completed || false,
};
localStorage.setItem('allGoals', JSON.stringify(allGoals));
});
});