Boa tarde! Tudo bem? Aqui está o meu projeto pronto para esta aula.
const form = document.getElementById('novoItem');
const list = document.getElementById('lista');
const items = JSON.parse(localStorage.getItem('items')) || [];
items.forEach((element) => {
createElement(element);
})
form.addEventListener('submit', (event) => {
event.preventDefault();
const name = event.target.elements['nome'];
const amount = event.target.elements['quantidade'];
const currentItem = {
'name': name.value, 'amount': amount.value
}
createElement(currentItem);
items.push(currentItem);
localStorage.setItem('items', JSON.stringify(items));
name.value = '';
amount.value = '';
})
function createElement(item) {
const newItem = document.createElement('li');
newItem.classList.add('item');
const itemNumber = document.createElement('strong');
itemNumber.innerHTML = item.amount;
newItem.appendChild(itemNumber);
newItem.innerHTML += item.name;
list.appendChild(newItem);
}