My problem is that the .click functions are like "bubbling" up. For example, if I stay on hand1, it goes to the hitOrStay2 for hand2, which is correct. However, if I click the hit button to hit on hand2, it hits on both hand1 and hand2, instead of just hand2. If I stay on both hand1 and hand2, but hit on hand3, it hits on all 3 hands.
Important Note. Yes I did have only 1 hitOrStay function and 1 tableOptions function, but it was doing the same thing. After messing around trying to fix it, I decided to have a different function for each of the 3 hands. However, the .hit, .stay html buttons are still the same, only one of each.
If I can't fix this, I guess my only option would be to create separate hit and stay buttons for each hand, with different ID's/classes...?
Here's my js/jQuery code: (I'm not looking for anyone to provide a working example, since there's too much that goes into this. I'd just like to know why this is happening. What are some ways to go about fixing it. Etc.)
function hitOrStay(cards, hand, nHands) {
var x = addValueOfCards(cards);
var rt = hand.classhtml.toString();
var choicemessage = $('<p class="choicemessage">You have ' + (x) + '. Do you want to hit or stay?</p>');
if (x < 19) {
$(choicemessage).appendTo('.message');
$('.hit, .stay').fadeIn();
$('.hit').click(function() {
$('.hit, .stay').hide();
cards.push(dealone(hand));
$('.message').find('.choicemessage').detach();
hitOrStay(cards, hand);
});
$('.stay').click(function() {
$('.hit, .stay').hide();
$('.message').find('.choicemessage').detach();
if (nHands > 1) {
tableOptions2(nHands); }
});
}
else {
$('<p>You Busted</p>').appendTo('.message');
$(rt).find('.crd').detach();
if (nHands > 1) {
tableOptions2(nHands); }
}
}
function hitOrStay2(cards, hand, nHands) {
var x = addValueOfCards(cards);
var rt = hand.classhtml.toString();
var choicemessage = $('<p class="choicemessage">You have ' + (x) + '. Do you want to hit or stay?</p>');
if (x < 19) {
$(choicemessage).appendTo('.message');
$('.hit, .stay').fadeIn();
$('.hit').click(function() {
$('.hit, .stay').hide();
cards.push(dealone(hand));
$('.message').find('.choicemessage').detach();
hitOrStay2(cards, hand);
});
$('.stay').click(function() {
$('.hit, .stay').hide();
$('.message').find('.choicemessage').detach();
if (nHands > 2) {
tableOptions3(nHands); }
});
}
else {
$('<p>You Busted</p>').appendTo('.message');
$(rt).find('.crd').detach();
if (nHands > 2) {
tableOptions3(nHands); }
}
}
function tableOptions(nHands) {
hitOrStay(hand1.cards, hand1, nHands);
}
function tableOptions2(nHands) {
hitOrStay2(hand2.cards, hand2, nHands);
}
function tableOptions3(nHands) {
hitOrStay3(hand3.cards, hand3, nHands);
}