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