I’m trying to make a little rock paper scissors game. for that I’ve written a small javascript in jquery. the whole thing should work like this: user presses button, an ajax event is fired, the selected option is added to a div and a counter increments. but only 3 times! I can’t figure it out to work like this… here’s my code
if (counter <= 3){
$('.buStein1').click(function(event){
$.post("game.php", { uid: "<?php echo $user['id']?>", choice: "stein", runde: counter });
$('#choiceContainer').append(stein);
counter++;
});
$('.buSchere1').click(function(event){
$.post("game.php", { uid: "<?php echo $user['id']?>", choice: "schere", runde: counter });
$('#choiceContainer').append(schere);
counter++;
});
$('.buPapier1').click(function(event){
$.post("game.php", { uid: "<?php echo $user['id']?>", choice: "papier", runde: counter });
$('#choiceContainer').append(papier);
counter++;
});
}
else {
$('#choiceContainer').text('no clicking possible');
}
First of all define your counter outside the functions:
Then check counter within the click event handlers prior to doing anything else: