Here is my code:

$(document).on('click', '.round', function() { var score = 0; score++; $('#result').html(score); this.remove(); }); 

It is necessary that when you click score increased by 1 and recorded in $('#result') .

But for some reason he only writes 1 and does not work anymore.

  • And isn't the score destroyed when exiting the circuit, being a local variable? Because of this, it is re-created and initialized each time the handler is called. - ߊߚߤߘ
  • I checked without this.remove(); same thing - Poctuk
  • @Arhad, there is no closure - Grundy
  • @Grundy, I apologize, confused the concepts of simply anonymous function and closure. - ߊߚߤߘ

1 answer 1

 $(document).on('click', '.round', function() { var score = 0; ^^^^^^^^^^^^^^ score++; $('#result').html(score); this.remove(); }); 

Your code shows that when you click on the button, the score variable is assigned 0 again;

Try to initialize above your function, or make it global, and then iterate by clicking on it.

 var score = 0; $(document).on('click', '.round', function() { score++; $('#result').html(score); this.remove(); }); 
  • Thanks helped! - Poctuk