something.appendChild is not a function error

15 Views Asked by At

Hi? I'm trying to build a note app by myself for the first time. I used createElement and appendChild to make a note in my noteContainer. and I got an error, "app.js:20 Uncaught TypeError: noteContainer.appendChild is not a function at HTMLButtonElement.addNewNote" I don't understand why it doesn't work.

    const btn = document.getElementById("add-btn");
    const noteContainer = document.getElementsByClassName("note-container");

    const addNewNote = () => {
        const newObj = {
            id: Math.floor(Math.random() * 10000),
            content: ""
        };

        const note = document.createElement("p");
        note.innerHTML = newObj.content;

        noteContainer.appendChild(note);
    
    };

    btn.addEventListener("click", addNewNote);

I tried to change noteContainer to document.body in addNewNote function, to find the problem. and it worked fine in that case. which could mean my noteContainer variable is the problem?? but I don't see any problem with it...

0

There are 0 best solutions below