-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategory.js
More file actions
78 lines (57 loc) · 2.05 KB
/
category.js
File metadata and controls
78 lines (57 loc) · 2.05 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
// The modal
var modal1 = document.getElementById("category-modal");
// Get the button that opens the modal
var btn1 = document.getElementById("open-category-modal");
// Get the <span> element that closes the modal
var span1 = document.getElementsByClassName("close-category-modal")[0];
// When the user clicks the button, open the modal
btn1.onclick = function () {
modal1.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span1.onclick = function () {
modal1.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
if (event.target == modal1) {
modal1.style.display = "none";
}
}
function newCategory() {
event.preventDefault()
const txt = document.getElementById('txt').value;
const colorPicker = document.getElementById("category-color").value;
const category = {
category: txt,
color: colorPicker
};
const ctlist = JSON.parse(window.localStorage.getItem('ctlist')) || [];
ctlist.push(category);
window.localStorage.setItem('ctlist', JSON.stringify(ctlist));
renderCategoryList();
// Clear the text field after submition
document.getElementById('txt').value = '';
modal1.style.display = "none";
}
// Render the user input to the page
function renderCategoryList() {
// Retrives the value of the localStorage item "ctlist" as a string (text)
const ctlist = JSON.parse(window.localStorage.getItem('ctlist')) || [];
const list = document.getElementById('ctlist');
list.innerHTML = '';
//console.log(ctlist);
for (const i in ctlist) {
const li = document.createElement('li');
li.style.color = `${ctlist[i].color}`;
li.innerHTML += `${ctlist[i].category}`;
list.appendChild(li);
}
}
renderCategoryList();
//When a window updates localStorage, other windows get notified through "addEventListener"
window.addEventListener("storage", function (event) {
if (event.key === "categoryList") {
renderCategoryList();
}
});