-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
46 lines (41 loc) · 1.19 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
42
43
44
45
46
'use strict';
const $form = document.querySelector('.form');
const $shoppingList = document.querySelector('.shpping-list');
const $inputList = document.querySelector('.input-list');
let totalNum = 0;
const createList = (text) => {
const $item = document.createElement('li');
const $text = document.createElement('span');
$text.innerText = text;
const $button = document.createElement('button');
$button.innerHTML = `<i class="fa-solid fa-trash-can"></i></button> `;
$item.append($text, $button);
$button.addEventListener('click', () => deleteList($item));
return $item;
};
const onAdd = () => {
const text = $inputList.value;
if (!text) {
$inputList.focus();
return;
}
const $list = createList(text);
totalNum++;
$inputList.value = '';
$inputList.focus();
$shoppingList.appendChild($list);
};
const showCount = () => {
const $totalNum = document.querySelector('.total-num');
$totalNum.innerText = `${totalNum ? totalNum : ''}`;
};
const deleteList = ($item) => {
$item.remove();
totalNum--;
showCount(totalNum);
};
$form.addEventListener('submit', (event) => {
event.preventDefault();
onAdd();
showCount();
});