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

224
Visualizações
Can't seem to find a way to clear my divs back to default (Etch a Sketch project)

I don't know if I'm doing the project "wrong" but so far this has been working. I'm basically just creating a JavaScript version of Etch a Sketch, where you hover your mouse over a div and it changes color. I need to create a button that resets the main div back to default, effectively "clearing" the page but nothing I try seems to work. Any suggestions?

const mainDev = document.createElement("div");
mainDev.style.width = "200px";
mainDev.style.height = "200px";
mainDev.style.display = "grid";
mainDev.id = "divId";
mainDev.style.gridTemplateColumns = "repeat(16, 1fr)";
let addMain = document.getElementById("main");
addMain.appendChild(mainDev);
for (let i = 0; i < 240; i++) {
    const sixteenDivs = document.createElement("div");
    sixteenDivs.classList.add("sixteen");
    sixteenDivs.style.backgroundColor = "white";
    sixteenDivs.style.width = "45px";
    sixteenDivs.style.height = "45px";
    sixteenDivs.style.border = "1px solid #000"
    sixteenDivs.style.display = "grid";
    sixteenDivs.style.margin = "5px 5px";
    let mouseOver = function() { sixteenDivs.style.backgroundColor = "rgb(20, 20, 20)" };
    sixteenDivs.onmouseover = mouseOver;
    mainDev.appendChild(sixteenDivs);
}

function clearDiv() {
    /// this is where I'm struggling
}
<div class="button">
    <button class="clearBtn" onclick="clearDiv()">CLEAR</button>
</div>

<div id="main"></div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

const mainDev = document.createElement("div");
mainDev.style.width = "200px";
mainDev.style.height = "200px";
mainDev.style.display = "grid";
mainDev.id = "divId";
mainDev.style.gridTemplateColumns = "repeat(16, 1fr)";
let addMain = document.getElementById("main");
addMain.appendChild(mainDev);
for (let i = 0; i < 240; i++) {
  const sixteenDivs = document.createElement("div");
  sixteenDivs.classList.add("sixteen");
  sixteenDivs.style.backgroundColor = "white";
  sixteenDivs.style.width = "45px";
  sixteenDivs.style.height = "45px";
  sixteenDivs.style.border = "1px solid #000"
  sixteenDivs.style.display = "grid";
  sixteenDivs.style.margin = "5px 5px";
  let mouseOver = function() {
    sixteenDivs.style.backgroundColor = "rgb(20, 20, 20)"
  };
  sixteenDivs.onmouseover = mouseOver;
  mainDev.appendChild(sixteenDivs);
}

function clearDiv() {
  mainDev.childNodes.forEach((child) =>
    child.style.backgroundColor = "white"
  )
}
<div class="button">
  <button class="clearBtn" onclick="clearDiv()">CLEAR</button>
</div>

<div id="main">

</div>

about 4 years ago · Juan Pablo Isaza Relatório
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