Saving reference to clicked item?

Saving reference to clicked item?

Hey all. I'm pretty new here and pretty stuck on a pretty small issue but it's escapes me. I have a bunch of divs and when you click one I want to save a reference to it, so when you click another it changes the first div back to it's original state.

Here's what I got so far...

<script>
      var lastTile;
   
       $(document).ready(function(){
         for(i = 0   ; i < 25; i++)
         {
            if(i % 5 == 0)
            {
               $("body").append("<div class='mapTileEnd'><img src='pink.png'/></div>");
            }else{
               $("body").append("<div class='mapTile'><img src='pink.png'/></div>");
         }
            }
         
         $(".mapTile").click(function() {
            $(this).replaceWith("<div class='mapTile'><img src='blue.png'/></div>");
         });
         
         $(".mapTileEnd").click(function() {
            $(this).replaceWith("<div class='mapTileEnd'><img src='blue.png'/></div>");
            resetOldTile(this);
         });

        });
       
        function resetOldTile(tile){
         if(lastTile != null)
         {
            $(lastTile).replaceWith("<div class='mapTileEnd'><img src='pink.png'/></div>");
         }
         lastTile = tile
        }

   </script>
   
   <style>
      .mapTile {
         float:left;padding:2px;
      }
      .mapTileEnd{
         float:left;padding:2px;clear:both;
      }
   </style>