2022-11-12 21:29:34 +00:00
|
|
|
document.addEventListener('DOMContentLoaded', function(){
|
2022-11-13 13:35:37 +00:00
|
|
|
const list = [];
|
2022-11-12 21:29:34 +00:00
|
|
|
|
2022-11-13 13:35:37 +00:00
|
|
|
const quantityInput = document.querySelector('.shopping-form-quantity-input');
|
2022-11-12 21:29:34 +00:00
|
|
|
const increment = document.querySelector('.shopping-form-increment-button');
|
|
|
|
const decrement = document.querySelector('.shopping-form-decrement-button');
|
2022-11-13 13:35:37 +00:00
|
|
|
const form = document.querySelector('.shopping-form');
|
|
|
|
const itemInput = document.querySelector('.shopping-form-item-input');
|
|
|
|
const items = document.querySelector('.shopping-items')
|
2022-11-12 21:29:34 +00:00
|
|
|
|
2022-11-13 13:35:37 +00:00
|
|
|
increment.addEventListener('click', addIcrement);
|
|
|
|
decrement.addEventListener('click', addDecrement);
|
|
|
|
form.addEventListener('submit', addTextValue);
|
2022-11-12 21:29:34 +00:00
|
|
|
|
|
|
|
function addIcrement() {
|
|
|
|
const valueQuantityInput = Number(quantityInput.value);
|
|
|
|
const newValue = valueQuantityInput + 1;
|
|
|
|
|
|
|
|
quantityInput.value = newValue;
|
|
|
|
}
|
|
|
|
function addDecrement() {
|
|
|
|
const valueQuantityInput = Number(quantityInput.value);
|
|
|
|
const newValue = valueQuantityInput - 1;
|
|
|
|
if(newValue > 0) {
|
|
|
|
quantityInput.value = newValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-11-13 13:35:37 +00:00
|
|
|
const ul = document.querySelector('ul')
|
2022-11-12 21:29:34 +00:00
|
|
|
const li = createElement('li');
|
|
|
|
|
|
|
|
|
2022-11-13 13:35:37 +00:00
|
|
|
function addTextValue(event) {
|
|
|
|
event.preventDefault();
|
2022-11-12 21:29:34 +00:00
|
|
|
|
2022-11-13 13:35:37 +00:00
|
|
|
const itemName = event.target['item-name'].value
|
|
|
|
const itemQuantity = event.target['item-quatity'].value
|
|
|
|
|
|
|
|
if(itemName != "") {
|
|
|
|
const item = {
|
|
|
|
nome: itemName,
|
|
|
|
quantity: itemQuantity
|
|
|
|
};
|
|
|
|
|
|
|
|
list.push(item);
|
|
|
|
renderListItems()
|
|
|
|
resetInput()
|
|
|
|
}
|
2022-11-12 21:29:34 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 13:35:37 +00:00
|
|
|
function renderListItems() {
|
|
|
|
let itemStructure = "";
|
|
|
|
|
|
|
|
list.forEach(function (item) {
|
|
|
|
itemStructure += `
|
|
|
|
<li class="shopping-item">
|
|
|
|
<span>${item.nome}</span>
|
|
|
|
<span>${item.quantity}</span>
|
|
|
|
</li>
|
|
|
|
`;
|
|
|
|
})
|
|
|
|
items.innerHTML = itemStructure
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function resetInput() {
|
|
|
|
itemInput.value = "";
|
|
|
|
quantityInput.value = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-12 21:29:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
})
|