Compare commits

..
4 Commits
Author SHA1 Message Date
Technoir 4f9a33baec feat: Gegenstände können nun entfernt werden
Gegenstände können nun entfernt werden. Dies wird erzielt durch einen
Knopf der dynamisch erstellt wird beim hinzufügen, dabei wird ihm ein
index übergeben welcher auf ihn selbst zeigt in listItems.
2026-09-28 15:44:07 +02:00
Technoir 2d68961487 refactor: bessere Lesbarkeit / Modularität
index.html: Umbenennung von Klassennamen
index.html: Funktionen und init() erstellt für Modularität und
Lesbarkeit
2026-09-28 15:42:31 +02:00
Technoir 54caa5faad hinzugefügt index.js
website kann Gegenstände zum kaufen hinzufügen
2026-09-28 15:42:31 +02:00
Technoir 8f848c6d9d index.html hinzugefügt
website hat einen Form mit welchem man Gegenstände hinzufügen kann
2026-09-28 15:42:31 +02:00
2 changed files with 47 additions and 17 deletions

No files matched your search

+3 -2
View File
@@ -9,12 +9,13 @@
</head>
<body>
<div class="title">
<h1>Einkaufsliste</h1>
</div>
<div class="list">
<form class="list__inputForm">
<input type="text" class="list__inputForm__itemName" name="nameOfItem">
<input type="text" name="amountOfItem" class="list__inputForm__itemAmount">
<input type="text" class="list__inputForm__itemName" name="itemName">
<input type="text" class="list__inputForm__itemAmount" name="itemAmount">
<button class="list__inputForm__submitButton" type="submit">Hinzufügen</button>
</form>
+44 -15
View File
@@ -1,20 +1,49 @@
const date = document.createElement("h1");
date.textContent = "Einkaufsliste vom: " + new Date().toLocaleString('de');
const host = document.querySelector(".title")
host.appendChild(date);
const form = document.querySelector(".list__inputForm");
const itemList = document.querySelector(".list__displayedList");
document.querySelector(".list__inputForm").addEventListener("submit", (event) => {
event.preventDefault();
addListElement();
});
let listItems = []; //list of items that the user has
function handleListElement(itemName, itemAmount,) {
listItems.push({ name: itemName, amount: itemAmount });
renderList();
function addListElement() {
var itemName = document.querySelector(".list__inputForm__itemName").value;
var itemAmount = document.querySelector(".list__inputForm__itemAmount").value;
var list = document.querySelector(".list__displayedList");
const newListElement = document.createElement("li");
newListElement.textContent = itemName + " " + itemAmount;
list.appendChild(newListElement);
return 0;
}
function renderList() {
itemList.innerHTML = ""; //reset innerItemList
listItems.forEach((element, index) => {
const li = document.createElement("li");
const removeButton = document.createElement("button");
removeButton.textContent = "Löschen";
removeButton.addEventListener("click", () => {
listItems.splice(index, 1);
renderList();
});
li.textContent = element.name + " " + element.amount;
li.appendChild(removeButton)
itemList.appendChild(li);
});
}
function init() {
document.querySelector(".list__inputForm").addEventListener("submit", (event) => {
if (!form.checkValidity()) return;
event.preventDefault();
formData = Object.fromEntries(new FormData(form));
handleListElement(formData.itemName, formData.itemAmount);
});
}
init();