Korey
Korey

Reputation: 5

How do I turn cards over using onClick?

Currently my cards are currently face down with the code* below. How would i go about changing them to face up using onClick functions? How would I then be able to use the ids to place in an Array and then use the IDs to get images from my images?

HTML

<table style="width:100%">
  <tr>
    <td><img src="cards_gif/b.gif" Class="back" id="0" onClick="flipCard()"></td>
    <td><img src="cards_gif/b.gif" Class="back" id="1" onClick="flipCard()"></td>
    <td><img src="cards_gif/b.gif" Class="back" id="2" onClick="flipCard()"></td>
  </tr>
  <tr>
   <td><img src="cards_gif/b.gif" Class="back" id="3" onClick="flipCard()"></td>
   <td><img src="cards_gif/b.gif" Class="back" id="4" onClick="flipCard()"></td>
   <td><img src="cards_gif/b.gif" Class="back" id="5" onClick="flipCard()"></td>
  </tr>
  <tr>
  <td><img src="cards_gif/b.gif" Class="back" id="6" onClick="flipCard()"></td>
  <td><img src="cards_gif/b.gif" Class="back" id="7" onClick="flipCard()"></td>
  <td><img src="cards_gif/b.gif" Class="back" id="8" onClick="flipCard()"></td>
  </tr>
  <tr>
  <td><img src="cards_gif/b.gif" Class="back" id="9" onClick="flipCard()"></td>
  <td><img src="cards_gif/b.gif" Class="back" id="10" onClick="flipCard()"></td>
  <td><img src="cards_gif/b.gif" Class="back" id="11" onClick="flipCard()"></td>
  </tr>
</table>

CSS

<style>
* {
    background-color: lightblue;
    border-color: hsla(173,100%,50%,1.00);
    font-weight: bold;  
}

.back {
    width:100px;
    height:100px;
}
</style>

JS

function flipCard(theElementID){
    /*var cardID = ["c1","c2","c3"];""3","4","5","6","7","8","9","10","11","12"]; *//ignore
    var theElement.src = document.getElementById(theElementID);
    theElement.src ="c1.gif";
    document.getElementById(c1.gif")
} // don't really understand this function. I also tried using an array to pick the card image if that makes sense.

Upvotes: 0

Views: 2569

Answers (1)

garryp
garryp

Reputation: 5766

Get rid of the onclick attribute which is needlessly repeated 12 times and wire a click event up to the image by css class in five lines of jQuery:

$(function() {
    $('.back').on('click', function() {
        flipCard(this.id); //Pass the img id to flipCard
    }); 
});

The original code has a few errors. See this fiddle which should give you some help: http://jsfiddle.net/GarryPas/gam8tcx7/2/

Upvotes: 2

Related Questions