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.
This commit is contained in:
Technoir committed 2026-09-28 15:44:07 +02:00
1 parent 2d68961487
commit 4f9a33baec
1 file changed
+29 -5
+29 -5
View File
@@ -6,18 +6,42 @@ let listItems = []; //list of items that the user has
function handleListElement(itemName, itemAmount,) {
var list = document.querySelector(".list__displayedList");
const newListElement = document.createElement("li");
newListElement.textContent = itemName + " " + itemAmount;
list.appendChild(newListElement);
listItems.push({ name: itemName, amount: itemAmount });
renderList();
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();
const formData = Object.fromEntries(new FormData(form));
formData = Object.fromEntries(new FormData(form));
handleListElement(formData.itemName, formData.itemAmount);
});
}