Removing all child nodes of a div with a while loop doesn’t work within event listener fucntion

So I am using the following code to remove all child nodes of a div.

    while (colorInputContainer.firstChild) {
        console.log(1);
        console.log(colorInputContainer);
        console.log(colorInputContainer.firstChild)
        colorInputContainer.removeChild(colorInputContainer.firstChild);
    }

This code in itself works inside the console. But when I use it in the full context (pastebin) it does not. And I have no idea why.