I am having trouble adding more than one flip card on a card game I'm working on. My code works well for one card, but since I'm using getElementById to make it work, it stops working when I'm adding more cards, since ID's are unique. Any idea? I want to achieve this with vanilla javascript.
HTML:
<body>
<div class="flexbox">
<div class="flip-card" style="margin: 100px 0 0 100px">
<div class="flip-card-inner">
<div class="flip-card-front" id="front" onclick="flip()">
<img
src="/images/strawman.png"
alt="Avatar"
style="width:100px;height:auto"
/>
<h2>The strawman</h2>
</div>
<div class="flip-card-back" id="back" onclick="flip()">
<div style="vertical-align: middle;">
<h3>The strawman</h3>
<p>
Transforming your opponent's argument into a caricature that you
can more easily attack
</p>
</div>
</div>
</div>
</div>
<div class="flip-card" style="margin: 100px 0 0 100px">
<div class="flip-card-inner">
<div class="flip-card-front" id="front" onclick="flip()">
<img
src="/images/strawman.png"
alt="Avatar"
style="width:100px;height:auto"
/>
<h2>The strawman</h2>
</div>
<div class="flip-card-back" id="back" onclick="flip()">
<div style="vertical-align: middle;">
<h3>The strawman</h3>
<p>
Transforming your opponent's argument into a caricature that you
can more easily attack
</p>
</div>
</div>
</div>
</div>
</div>
Javascript :
<script>
let toggle = true;
function flip() {
if (toggle == true) {
document.getElementById("front").style.transform = "rotateY(180deg)";
document.getElementById("back").style.transform = "rotateY(0deg)";
toggle = !toggle;
console.log(toggle);
} else {
document.getElementById("front").style.transform = "rotateY(0deg)";
document.getElementById("back").style.transform = "rotateY(-180deg)";
toggle = !toggle;
console.log(toggle);
}
}
</script>
Thanks for the help :)