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

231
Views
How to add a class to card and remove once clicked outside

So I have a couple of cards that will be cloned and I want to add a class when clicked inside those cards and replace it when clicked outside and I want to keep a certain class called metadata unchanged

my fiddle https://jsfiddle.net/abdotamer3/s57jauxw/27/

My detection function:

$(".card").on("click", function (e) {
    $(this).removeClass("viewItemInactive");
    $(this).addClass("viewItemActive");
});
$(document).on("click", function (e) {
    if ($(e.target).is(".card") === false) {
        $(".card").not(".MetaData").each(function() {
            $(this).removeClass("viewItemActive");
            $(this).addClass("viewItemInactive");
        });
    }
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If i understand you correctly, you can just add this 2 lines to on click

$(".card").removeClass('randomClass');
$(this).addClass('randomClass');

So this will add class when you click on a card, and remove it when you click on another card and also add it again to the one you clicked.

So the on click would than be like this:

$(".card").on("click", function (e) {
    $(this).removeClass("viewItemInactive");
    $(this).addClass("viewItemActive");
    $(".card").removeClass('randomClass');
    $(this).addClass('randomClass');
});
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!