0

How do I make an image clickable to run the displayScore function?

$(function(){
   $(".Image").on('click',function(){
      displayScore();
   }); 
});

function addMole(){
   $("#gamespace").append('<img class= 'Image' img 
   src="img/mole.png"/>');
   moreMoles = setTimeout("addMole()", 2000);
 };
Andrea
  • 1
  • 1
  • 1
    Possible duplicate of [Event binding on dynamically created elements?](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – David Nov 14 '17 at 13:31
  • Part of your problem is the use of single quotes on the markup in the `append` method call, which serves to truncate the argument string. Fix your syntax and this will probably work. My personal approach is single for JS and double for HTML. This avoids many problems. – isherwood Nov 14 '17 at 14:46
  • Thank you, the single quote was my issue – Andrea Nov 14 '17 at 17:10

1 Answers1

0
$(function(){
   $(document).on('click','.Image',function(){
      displayScore();
   }); 
});
Irfan TahirKheli
  • 3,652
  • 1
  • 22
  • 36