Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda