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
Static cards won't flip when "flip" function called

I have been trying for hours to get my playing cards to flip when the player clicks on them. When the eventListener is fired it should run the flip function which toggles between the front/backsides of a card. What am I doing wrong?

const playingCards = document.querySelectorAll('.playing-cards');

function flipCard(){;
    this.classList.toggle("flip")
}

playingCards.forEach((card) => card.addEventListener("click", flipCard));
.playing-cards{
    display: inline-block;
    padding: 20px;
    position: relative;
    cursor: pointer;
}
.playing-cards .flip{
    transform: rotateY(180deg);
}
.front-card{
    background-color: aqua;
    width: 300px;
    height: 300px;
    transform: rotateX(0deg);
}
.back-card{
    background-color: greenyellow; 
    width: 300px;
    height: 300px;
    transform: rotateY(180deg);
}
.front-card,
.back-card{
    width: 100%;
    height: 100%;
    position: absolute;
    backface-visibility: hidden;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="index.css">
    <title>Document</title>
</head>

<div class="memory-game">
    <div class="playing-cards">
        <div class="front-card">A</div>
        <div class="back-card"></div>
    </div>

    <div class="playing-cards">
        <div class="front-card">B</div>
        <div class="back-card"></div>
    </div>

    <div class="playing-cards">
        <div class="front-card">a</div>
        <div class="back-card"></div>
    </div>

    <div class="playing-cards">
        <div class="front-card">b</div>
        <div class="back-card"></div>
    </div>


</div>


<body>
    <script src=index.js></script>
</body>
</html>

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

0

Answer derived from here

const playingCards = document.querySelectorAll('.playing-cards');

function flipCard() {
  this.classList.toggle("flip")
}

playingCards.forEach((card) => card.addEventListener("click", flipCard));
/* The flip card container - set the width and height to whatever you want. We have added the border property to demonstrate that the flip itself goes out of the box on hover (remove perspective if you don't want the 3D effect */
.memory-game {
  background-color: transparent;
  border: 1px solid #f1f1f1;
  perspective: 1000px; /* Remove this if you don't want the 3D effect */
}

/* This container is needed to position the front and back side */
.playing-cards {
display: inline-block;
  position: relative;
  width: 30px;
  height: 30px;
  text-align: center;
  transition: transform 0.8s;
  transform-style: preserve-3d;
  cursor: pointer;/*clickable-cursor*/
}

/* Do an horizontal flip when you move the mouse over the flip box container */
.playing-cards.flip {
  transform: rotateY(180deg);
}

/* Position the front and back side */
.front-card, .back-card {
  position: absolute;
  width: 100%;
  height: 100%;
  -webkit-backface-visibility: hidden; /* Safari */
  backface-visibility: hidden;
}

/* Style the front side (fallback if image is missing) */
.front-card {
  background-color: aqua;

  color: black;
}

/* Style the back side */
.back-card {
  background-color: greenyellow;
  color: white;
  transform: rotateY(180deg);
}
<div class="memory-game">
  <div class="playing-cards">
    <div class="front-card">A</div>
    <div class="back-card"></div>
  </div>

  <div class="playing-cards">
    <div class="front-card">B</div>
    <div class="back-card"></div>
  </div>

  <div class="playing-cards">
    <div class="front-card">a</div>
    <div class="back-card"></div>
  </div>

  <div class="playing-cards">
    <div class="front-card">b</div>
    <div class="back-card"></div>
  </div>
</div>

Edit: Added cursor effect

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!