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

145
Views
Multiple button click animation jQuery

I am beginner so please don't make too much fun of me, but I am having issues trying to get separate buttons to react when clicked. The current code that I have written will dim the all of the buttons when I click on only one of them. The problem is that I have 4 buttons and I want them to all be independent, but I don't want to type the same code 4 times for each of them.


Here is my HTML:

  <div type="button" id="green" class="btn green">

  </div>

  <div type="button" id="red" class="btn red">

  </div>
</div>

<div class="row">

  <div type="button" id="yellow" class="btn yellow">

  </div>
  <div type="button" id="blue" class="btn blue">

  </div>

</div>

Here is my jQuery code:

let buttonDim = function(){
  $(".btn").on("click", function(){
    $(".btn").addClass("pressed");
    setTimeout(function(){
      $(".btn").removeClass("pressed");
    }, 100);
  });
}
buttonDim();

I am aware that I have probably written the code ridiculously longer than it needs to be, but I am still learning. Any help would be greatly appreciated, thank you.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try js buttonDim() like this

 $(".btn").on("click", function(){
  var button=$(this);
   button.addClass("pressed");
   setTimeout(function()
 {
    button.removeClass('pressed');
        
 }, 2000);
});
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!