-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddRemoveSaveEditFunctions.js
More file actions
54 lines (44 loc) · 1.58 KB
/
Copy pathaddRemoveSaveEditFunctions.js
File metadata and controls
54 lines (44 loc) · 1.58 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
function addItem(e) {
e.preventDefault();
var text = (this.querySelector('[name=item]')).value,
item = {
text,
done: false
};
items.push(item);
localStorage.setItem('items', JSON.stringify(items));
populateList(items, itemsList);
this.reset();
}
function removeItem (e) {
items.splice(e.target.dataset.index,1);
localStorage.setItem('items', JSON.stringify(items));
populateList(items, itemsList);
}
function editItem (e) {
var index = e.target.dataset.index,
label = document.querySelectorAll('label')[index],
saveButton = document.querySelectorAll('.save')[index],
editButton = document.querySelectorAll('.edit')[index],
editInput = document.querySelectorAll('.editInput')[index];
editInput.style.display = 'inline-block';
editInput.value = label.innerText;
saveButton.style.display = 'block';
editButton.style.display = 'none';
}
function saveItem(e) {
var index = e.target.dataset.index,
label = document.querySelectorAll('label')[index],
saveButton = document.querySelectorAll('.save')[index],
editButton = document.querySelectorAll('.edit')[index],
editInput = document.querySelectorAll('.editInput')[index];
editButton.style.display = 'block';
saveButton.style.display = 'none';
editInput.style.display = 'none';
label.innerHTML = editInput.value;
items[index].text = label.innerText;
if(editInput.value === ''){
return removeItem(e) ;
}
localStorage.setItem('items', JSON.stringify(items))
}