-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
41 lines (33 loc) · 1.02 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
console.log('### TO DO LIST ###');
const listContainer = document.querySelector('[data-lists]');
const newListForm = document.querySelector('[data-new-list-form]');
const newListInput = document.querySelector('[data-new-list-input]');
let lists = [];
newListForm.addEventListener('submit', function(e) {
e.preventDefault();
const listName = newListInput.value
if( listName === null || listName === '' ) return
const list = createList(listName);
newListInput.value = null;
newListInput.focus();
lists.push(list);
render();
});
function createList(name) {
return {id: Date.now().toString(), name: name }
}
function render() {
clearElement(listContainer);
lists.forEach(function(list){
const item = document.createElement('li');
item.classList.add('item');
item.innerText = list.name;
listContainer.appendChild(item);
});
}
function clearElement(element) {
while (element.firstChild) {
element.removeChild(element.firstChild);
}
}
render();