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

73
Visualizações
How to write a function that will return colors at random for a drawing pad

I am creating an simple etch a sketch design and need to create function that will input randomized colors upon hovering over the grid. So far I have created functions that some specific colors but I'm not sure where to go from there.

const grid = document.querySelector('.grid');

const blackButton = document.querySelector('.blackbutton');
blackButton.addEventListener('click', blkButton);

const clear = document.querySelector('.clearbutton')
clear.addEventListener('click', clearGrid);

const eraserButton = document.querySelector('.eraserbutton')
eraserButton.addEventListener('click', ersButton)

const rainbowButton = document.querySelector('.rainbowbutton')
rainbowButton.addEventListener('click', rbwButton)


function getGrid(gridNumber) {
  for (let i = 1; i <= gridNumber * gridNumber; i++) {
   const box = document.createElement('div');
   box.classList.add('box');
   grid.appendChild(box);
  }
 }

getGrid(16);


 function blkButton() {
 grid.querySelectorAll(".box").forEach(box => 
box.addEventListener("mouseover", () => {
box.style.background = "black";
 }))
}


function ersButton(){
  grid.querySelectorAll(".box").forEach(box => 
  box.addEventListener("mouseover", () => {
  box.style.background = "lightgray"
  }))
  }

  function rbwButton(){
  }




   function clearGrid() {
   const tgt = ".box"
   grid.querySelectorAll(".box").forEach(box => 
   box.removeAttribute('style') )
   }
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

I did something very similar in Angular which I have shown here. This is for reference. Please modify syntax if required.

function blkButton() {
  grid.querySelectorAll(".box").forEach(box => 
  box.addEventListener("mouseover", () => {
  box.style.background = this.getColor();
 }))
}

 rgb = [];
 function getColor(){
    for (var i = 0; i < 3; i++) {
        this.rgb.push(Math.floor(Math.random() * 255));
   }
  return 'rgb(' + this.rgb.join(',') + ')';
 }
about 4 years ago · Santiago Gelvez 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