Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

299
Visualizações
trying to reuse a function to append stuff into the DOM, appendChild not working as spected

So i have this function:

export function Home() {
 const moduleContainer = document.createElement("div");
 const uiContainer = document.querySelector("#ui-container");
 uiContainer.innerHTML = "";
 moduleContainer.id = "module-container";
 moduleContainer.classList.add("module-container", "min-height");
 const removeStats = statsUI(gameState.player, uiContainer);
 const removeInv = inventory(gameState.player, uiContainer);
 const battleBossButton = createButton("Boss", "Batalla", ()=>{
    Battle(bossMonsters.squeletonKing, "Pradera");
 });
 const battleButton = createButton("Mob", "Batalla", ()=>{
    Battle(monsters.goblin, "Montaña");
 });
 const shopButton = createButton("Shop", "Tienda", ()=>{
    Shop();
 });
 moduleContainer.appendChild(battleButton);
 moduleContainer.appendChild(battleBossButton);
 moduleContainer.appendChild(shopButton);
 mainContainer.appendChild(moduleContainer);
}

The first time i run this function everything works fine but when i (using a different function) empty the mainContainer and rerun this function the moduleContainer doesn't get appended again, the thing is that if i console.log the moduleContainer it does have the buttons inside of it, but the mainContianer doesen't have anything inside, i'm not sure what is going on, any ideas?

To clarify what i mean i took this screenshot enter image description here

as you can see the first time it runs the dom looks correct the second time main container has nothing inside.

EDIT: Main container is defined in a constants file and exported like this export const mainContainer = document.querySelector("#main-container"); Also createButton function comes from a helper file and looks like this

export function createButton(text, id, callback) {
 const button = document.createElement("button");
 button.id = id;
 button.classList.add("btn", "btn-custom");
 button.innerText = text;
 button.addEventListener("click", () => callback());
 return button;
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda