-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
51 lines (44 loc) · 1.63 KB
/
Copy pathscript.js
File metadata and controls
51 lines (44 loc) · 1.63 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
initial();
async function initial() {
const list = await getList();
createHtml(list);
document.getElementById('ul-elem').addEventListener('click', deleteItem);
document.getElementById('butt-elem').addEventListener('click', addText);
}
async function getList() {
const response = await fetch('https://jsonplaceholder.typicode.com/todos');
const list = await response.json();
console.log(list);
return list;
}
function createHtml(arrList) {
const divEl = document.body.appendChild(document.createElement('div'));
const inputEl = divEl.appendChild(document.createElement('input'));
inputEl.setAttribute('id', 'input-elem');
const buttonEl = divEl.appendChild(document.createElement('button'));
buttonEl.setAttribute('id', 'butt-elem');
buttonEl.innerText = 'add';
const ulEl = divEl.appendChild(document.createElement('ul'));
ulEl.setAttribute('id', 'ul-elem');
arrList.forEach(item => {
const liEl = ulEl.appendChild(document.createElement('li'));
const spanEl = liEl.appendChild(document.createElement('span'));
spanEl.innerText = item.title;
});
}
function deleteItem() {
if (event.target.tagName === 'SPAN') {
const span = event.target.parentNode;
span.parentNode.removeChild(span);
}
}
function addText() {
const inputEl = document.getElementById('input-elem');
if (inputEl.value.length === 0) {
alert('Please fill in the field');
};
const ulEl = document.getElementById('ul-elem');
const liEl = ulEl.appendChild(document.createElement('li'));
liEl.innerText = inputEl.value;
inputEl.value = '';
}