I'm recreating a classic card memory game for my final project - a deck of cards that I need to flip and match. An extract of the deck of cards in HTML is below:
<ul class="deck">
<li class="card">
<i class="fa fa-diamond"></i>
</li>
<li class="card">
<i class="fa fa-paper-plane-o"></i>
</li>
<li class="card">
<i class="fa fa-diamond"></i>
</li>
<li class="card">
<i class="fa fa-paper-plane-o"></i>
</li>
</ul>
I've already added the flip functionality by applying event delegation to the parent node and using the event.target property.
document.querySelector('.deck').addEventListener('click', function(event) {
if (event.target.nodeName === 'LI') {
event.target.classList.add('open');
//get child element "i" classname, add to array and match cards
}
});
Now I need to get the child of the card that was clicked, add that to an array, and then use that array to match two open cards together. Problem is, I can't seem to figure out how to get the child element's class (e.g. i class="fa fa-diamond"). How can I proceed from here?
I thought about just adding an individual event listener to each card instead, but I guess that would be a more costly operation. Or would this be better instead? Looking forward to your help and suggestions.