Zagadnienie: Dodawanie elementów do listy

Na egzaminie czasem trzeba dodać nowy element (li, p, wiersz tabeli) po kliknięciu przycisku.

Schemat:
1. createElement("li") → tworzy nowy element
2. element.innerHTML = "tekst" → ustawia treść
3. lista.appendChild(element) → dodaje na koniec

Wersja A – najprostsza (egzaminowa)

function dodajA() {
    let tekst = document.getElementById("produktA").value;
    if (tekst === "") return;               // nie dodawaj pustego

    let nowy = document.createElement("li");
    nowy.innerHTML = tekst;
    document.getElementById("listaA").appendChild(nowy);

    document.getElementById("produktA").value = "";  // wyczyść pole
}

Wersja B – addEventListener

const inputB = document.getElementById("produktB");
const listaB = document.getElementById("listaB");

document.getElementById("btnB").addEventListener("click", function() {
    let tekst = inputB.value;
    if (tekst === "") return;

    let nowy = document.createElement("li");
    nowy.innerHTML = tekst;
    listaB.appendChild(nowy);
    inputB.value = "";
});

Wersja C – kompaktowa

document.getElementById("btnC").onclick = () => {
    const tekst = document.getElementById("produktC").value;
    if (!tekst) return;

    const li = document.createElement("li");
    li.innerHTML = tekst;
    document.getElementById("listaC").appendChild(li);
    document.getElementById("produktC").value = "";
};