From d654a393f059c1ce96790d4b48d104195bc69327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cain=C3=A3=20Milech?= Date: Tue, 8 Nov 2022 11:06:39 -0300 Subject: [PATCH] feat(app.js): implementa functions para criar objeto --- index.html | 2 ++ scripts/app.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 scripts/app.js diff --git a/index.html b/index.html index 6091f32..9005ffc 100644 --- a/index.html +++ b/index.html @@ -38,5 +38,7 @@ + + \ No newline at end of file diff --git a/scripts/app.js b/scripts/app.js new file mode 100644 index 0000000..bf67fc2 --- /dev/null +++ b/scripts/app.js @@ -0,0 +1,48 @@ +document.addEventListener('DOMContentLoaded', function(){ + const list = []; //criar array vazio para adicionar os itens nele + + const form = document.querySelector('.shopping-form'); + const itemInput = document.querySelector('.shopping-form-item-input'); + + const quantityInput = document.querySelector('.shopping-form-quantity-input'); + const incrementButton = document.querySelector('.shopping-form-increment-button'); + const decrementButton = document.querySelector('.shopping-form-decrement-button'); + + incrementButton.addEventListener("click", incrementQuantity); + decrementButton.addEventListener("click", decrementQuantity); + form.addEventListener("submit", addItemToList); + + function incrementQuantity(){ + const currentValue = Number(quantityInput.value); //era string, passa ser numero + const newValue = currentValue +1; //adiciona +1 + + quantityInput.value = newValue; //adiciona o num atualizado no input + } + + function decrementQuantity(){ + const currentValue = Number(quantityInput.value); //era string, passa ser numero + const newValue = currentValue -1; //adiciona +1 + + if(newValue > 0){ + quantityInput.value = newValue; //adiciona o num atualizado no input + } + } + + function addItemToList(event){ + event.preventDefault(); //evitar o padrao, que é atualizar a pag e passar parametros na url + + const itemName = event.target["item-name"].value; //pegar valor do input name + const itemQuantity = event.target["item-quantity"].value; //pegar valor do input quantity + + if(itemName !== ""){ //se o input nao estiver vazio, criar um objeto + const item = { + name: itemName, + quantity: itemQuantity, + }; //criar objeto com os parametros recebidos + + list.push(item); //adicionar o objeto no array + } + + + } +}) \ No newline at end of file