diff --git a/index.html b/index.html index 2487417..4220a72 100644 --- a/index.html +++ b/index.html @@ -20,7 +20,7 @@

Shopping List

-
+
@@ -38,10 +38,11 @@ class="shopping-from-quantity-button shopping-form-decrement-button" type="button" > - + + - - - + +
@@ -61,7 +62,16 @@ Add to list
+ +
+
+ Item + Quantity +
+
    +
    + diff --git a/scripts/app.js b/scripts/app.js new file mode 100644 index 0000000..5eb4c2c --- /dev/null +++ b/scripts/app.js @@ -0,0 +1,67 @@ +document.addEventListener('DOMContentLoaded', function () { + const list = [] + + const form = document.querySelector('.shopping-form'); + const itemInput = document.querySelector('.shopping-form-item-input') + const quantifyInput = document.querySelector('.shopping-form-quantity-input'); + const incrementButton = document.querySelector('.shopping-form-increment-button'); + const decrementButton = document.querySelector('.shopping-form-decrement-button'); + const items = document.querySelector('.shopping-items'); + + incrementButton.addEventListener("click", incrementQuantity); + decrementButton.addEventListener("click", decrementQuantity); + form.addEventListener("submit", addItemToList); + + function incrementQuantity() { + const currentValue = Number(quantifyInput.value); + const newValue = currentValue + 1; + + quantifyInput.value = newValue; + } + + function decrementQuantity() { + const currentValue = Number(quantifyInput.value); + const newValue = currentValue - 1; + + if (newValue > 0) { + quantifyInput.value = newValue; + } + } + + function addItemToList(event) { + event.preventDefault(); + + const itemName = event.target["item-name"].value; + const itemQuantify = event.target["item-quantity"].value + + if (itemName !== "") { + const item = { + name: itemName, + quantity: itemQuantify + } + list.push(item); + renderListItems(); + resetInputs(); + } + } + + function renderListItems() { + let itemsStructure = ""; + + list.forEach(function (item) { + itemsStructure += ` +
  • + ${item.name} + ${item.quantity} +
  • + ` + }); + + items.innerHTML = itemsStructure; + } + + function resetInputs() { + itemInput.value = ""; + quantifyInput.value = 1; + } +}) \ No newline at end of file