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

176
Views
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 answers
Answer question

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 Report
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!