Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

307
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!