I am learning Javascript and have been trying to make a simple flashcard game, but am getting hung up on what I thought would be the pretty simple task of getting a card to flip over. I absolutely cannot understand why this is not working. I can get the card to “flip” from the front to back onclick, but not back to front on the subsequent click and it really seems like my code should work. Anyone out there able to take a look and tell me what I’m doing wrong? The Html below is followed by the javascript.
i think you have an issue with the scope that card operates in. maybe you want to reset the card variable everytime the function is called. other wise its remains unchanged. i could be wrong but try that.
// var card = document.getElementById('image');
notecard.addEventListener('click', flipCard);
flipCard(){
let card = document.getElementbyid('image');
let front = "assets/front.png"
let back = "assets/back.png"
if (card.src == front) {
card.src = back
} else {
card.src = front
}
}
Thanks for this. I didn’t have any luck with it — it did the same thing (wouldn’t let the back card flip back to the front) — but I really appreciate your time. I guess it’s just bad coding and I’ll find another way to do it. Still, it dumbfounds me as I don’t understand why it doesn’t work.
Another thing to look out for is that the src is pointing to the right path. Depending on where the image is being serve, you’ll have to include the host. Though if it is localhost, browsers are smart enough to look in your file system with ./assets/