So I'm appending circles to an arena which is randomized.
The problem I am having is that once the circle is appended the onclick does not work on the circle.
This is what I've tried:
manyBalls = setInterval(function() {
$("<div class='circle-gm-2'></div>").appendTo(".arena-2").css({
backgroundColor: circleColor,
width:(circleRange).val(),
height:(circleRange).val(),
//Here comes the random magic position of the circle
marginLeft: Math.floor(Math.random() * arenaWidth),
marginTop: Math.floor(Math.random() * arenaHeight)
});
}, 300,function(){
$(this).on("click", function(e){
//When the user clicks on the circle inside the arena shit will happen
//Stopping the Propagation because we are counting misses if the user presses the arena and the circle is a child of it...
e.stopPropagation();
hits++;
//Counting how many hits
$(".totalhits").text(hits);
//Removing the circle
$(this).remove();
});
});
Also this does not work:
$(".circle-gm-2").on("click", function(e){
//When the user clicks on the circle inside the arena shit will happen
//Stopping the Propagation because we are counting misses if the user presses the arena and the circle is a child of it...
e.stopPropagation();
hits++;
//Counting how many hits
$(".totalhits").text(hits);
//Removing the circle
$(this).remove();
});
Because it will add an eventlistener on all the ".circle-gm-2" showing each time.
How do I make it so ONLY the current appended circle to the arena, is added an eventlistener.