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

177
Visualizações
Adding class when screen is clicked

I have no knowledge of js, but I am trying to add a class to a div when any point of the screen is clicked. I have this html

<div class="container">
   <div class="box"></div>
</div>

And I need to add the class to the container when any point of the screen is clicked.

I tried this js

<script>
let button = document.querySelector('.container');
button.addEventListener('click', function() {
button.classList.add('.hide');
});
</script>

but it is not working, the class is not being applied. What am I doing wrong?

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

0

There is a small syntax error: the class should be mentioned without the dot prefix:

button.classList.add('hide');

Demo

let button = document.querySelector('.container');

button.addEventListener('click', function() {
    button.classList.add('hide');
});
.container {
  background-color: #ddd;
  cursor: pointer;
}

.box {
  background-color: #ccf;
  margin: 1rem;
}

.hide {
  opacity: 0;
  transition: opacity .5s ease-in-out;
}
<div class="container">
<br>  
   <div class="box">Click anywhere in the grey container to hide it</div>
   <br>  
</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