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

163
Views
jQuery - triggering two sequential click events more than once

When I first click on a box it enlarges and when I click it again it shrinks back to its original size. This applies for all the boxes. But when I click on a box that has gone through these two events it does not grow again. I want all my boxes to always be able to grow and shrink back to their original sizes, not just once. Have a look at this fiddle

$(".item").on("click", function() {
        $(this).addClass("grow");
        $(".item").not(this).each(function() {
                $(this).addClass("collapse");
        })
        $(this).on("click", function() {
                $(this).removeClass("grow");
                $(".item").not(this).each(function() {
                        $(this).removeClass("collapse");
                })
        })
})

Does anyone have a solution to this issue?

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

0

Why not checking for .grow class first? ↓ DEMO fiddle

$(".item").on("click", function() {
    $(this).toggleClass("grow");
    $(".item").not(this).toggleClass("collapse");
})
about 4 years ago · Juan Pablo Isaza Report

0

You don't need to write so long code. You can do this easily by using toggleClass() method. You even don't need collapse class to take box back to its normal position.

You can write your code as:

$(".item").on("click", function(){
   $(this).toggleClass("grow");
});
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!