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

143
Views
Click event not returning the correct element properly

I am trying to create a card that rotates on click. It is working but I want to use the onclick event to add the 'is-flipped' class to the classlist of div.card. When I console log the event it shows target as <div class="card__face card__face--front">front</div> and currentTarget as null. I thought this was due to absolute positioning so I tried z-index: 100 still not working. Why is this happening and how can I get the .card div.

// var card = document.querySelector(".card");
// card.addEventListener("click", function () {
//     card.classList.toggle("is-flipped");
// });

function handleCardClick(e) {
    // e.stopPropagation();
    console.log(e);
}
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}
body {
    font-family: "Poppins", sans-serif;
}

body {
    font-family: sans-serif;
}

.wrapper {
    width: 200px;
    height: 260px;
    border: 1px solid #ccc;
    margin: 40px 0;
    perspective: 600px;
}

.card {
    width: 100%;
    height: 100%;
    transition: transform 1s;
    transform-style: preserve-3d;
    cursor: pointer;
    position: relative;
    z-index: 100;
}

.card.is-flipped {
    transform: rotateY(180deg);
}

.card__face {
    position: absolute;
    width: 100%;
    height: 100%;
    line-height: 260px;
    color: white;
    text-align: center;
    font-weight: bold;
    font-size: 40px;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

.card__face--front {
    background: red;
}

.card__face--back {
    background: blue;
    transform: rotateY(180deg);
}
<div class="wrapper">
    <div class="card" onclick="handleCardClick(event)">
        <div class="card__face card__face--front">front</div>
        <div class="card__face card__face--back">back</div>
    </div>
</div>

<div class="main" onclick="handleCardClick(event)">123456789</div>

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

in order to manipulate with classes on 'card' element try:

const card = document.querySelector('.card');
card.addEventListener('click', function(){
 card.classList.toggle('is-flipped')
})

if you want to use onclick="" then you should add "pointer-events: none;" property to .card child elements

.card__face {
pointer-events: none;}

and

function handleCardClick(e) {

const currentEl = e.target;
currentEl.classList.toggle('is-flipped')}
about 4 years ago · Santiago Gelvez 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!