You associate events with a few clicks when a function call is ever called
function spawnTargets() { $(".wrap").append("<div class='target'>Hello</div>"); $(".target").click(function(){ $(this).hide(); addScore(); }); }
Instead you need to delegate the event
Change
$(".target").click(function(){
to
$('.wrap').on("click", ".target", function(){
And move it out of function
code
var score = 0; // Starting score for game function addScore() { score++; console.log(score); } function spawnTargets() { $(".wrap").append("<div class='target'>Hello</div>"); } $('.wrap').on("click", ".target", function () { $(this).hide(); addScore(); }); $(".go").click(function () { $(".intro").toggle(); setInterval(spawnTargets, 1000); });
source share