I am currently trying to develop a prototype using jQuery that makes a whole stack of cards draggable within the window once dealt. I am noticing that the entire stack is not getting the class=ui.draggable
property within my console on my website. I am not sure as to why only some of these cards within the array would be getting the property seemingly at random each time. Would appreciate any feedback on this!
$('#deal').click(function () {
dealCard(randomCard());
});
$( function init() {
$(".drop").droppable( { drop: dropCard } );
} );
function dropCard(event, ui) {
$('#drop').html( 'The card "' + ui.draggable.attr('id') + '" was dropped.' );
}
var cardsInDeck = new Array();
var numberOfCardsInDeck = 51;
cardsInDeck[0] = "ClubAce";
cardsInDeck[1] = "Clubs2";
cardsInDeck[2] = "Clubs3";
cardsInDeck[3] = "Clubs4";
cardsInDeck[4] = "Clubs5";
cardsInDeck[5] = "Clubs6";
cardsInDeck[6] = "Clubs7";
cardsInDeck[7] = "Clubs8";
cardsInDeck[8] = "Clubs9";
cardsInDeck[9] = "Clubs10";
cardsInDeck[10] = "ClubsJack";
cardsInDeck[11] = "ClubsKing";
cardsInDeck[12] = "ClubsQueen";
cardsInDeck[13] = "DiamondsAce";
cardsInDeck[14] = "Diamonds2";
cardsInDeck[15] = "Diamonds3";
cardsInDeck[16] = "Diamonds4";
cardsInDeck[17] = "Diamonds5";
cardsInDeck[18] = "Diamonds6";
cardsInDeck[19] = "Diamonds7";
cardsInDeck[20] = "Diamonds8";
cardsInDeck[21] = "Diamonds9";
cardsInDeck[22] = "Diamonds10";
cardsInDeck[23] = "DiamondsQueen";
cardsInDeck[24] = "DiamondsJack";
cardsInDeck[25] = "DiamondsKing";
cardsInDeck[26] = "HeartsAce";
cardsInDeck[27] = "Hearts2";
cardsInDeck[28] = "Hearts3";
cardsInDeck[29] = "Hearts4";
cardsInDeck[30] = "Hearts5";
cardsInDeck[31] = "Hearts6";
cardsInDeck[32] = "Hearts7";
cardsInDeck[33] = "Hearts8";
cardsInDeck[34] = "Hearts9";
cardsInDeck[35] = "Hearts10";
cardsInDeck[36] = "HeartsJack";
cardsInDeck[37] = "HeartsKing";
cardsInDeck[38] = "HeartsQueen";
cardsInDeck[39] = "SpadesAce";
cardsInDeck[40] = "Spades2";
cardsInDeck[41] = "Spades3";
cardsInDeck[42] = "Spades4";
cardsInDeck[43] = "Spades5";
cardsInDeck[44] = "Spades6";
cardsInDeck[45] = "Spades7";
cardsInDeck[46] = "Spades8";
cardsInDeck[47] = "Spades9";
cardsInDeck[48] = "Spades10";
cardsInDeck[49] = "SpadesJack";
cardsInDeck[50] = "SpadesQueen";
cardsInDeck[51] = "SpadesKing";
function dealCard(i) {
if (numberOfCardsInDeck === 0) return false;
var img = document.createElement("img");
img.setAttribute("id", cardsInDeck[i]);
img.setAttribute("height", "100px");
img.src ="../Cards/" + cardsInDeck[i] + ".png";
document.body.appendChild(img);
console.log(cardsInDeck[i]);
removeCard(i);
$("#" + cardsInDeck[i]).draggable({ containment: 'document' });
}
function randomCard() {
return Math.floor(Math.random() * numberOfCardsInDeck);
}
function removeCard(c)
{
for (j=c; j <= numberOfCardsInDeck - 2; j++)
{
cardsInDeck[j] = cardsInDeck[j+1];
}
numberOfCardsInDeck--;
}
2
Answers
Consider this part of your snippet:
Within
dealCard()
, when you callremoveCard(i)
(second last line), it changes the content ofcardsInDeck
. Because of this change, when you usecardsInDeck
in the next (last) line you don’t always get the correct id.Possible fixes: store the
id
(cardsInDeck[i]
) before callingremoveCard(i)
and use it later. Alternatively, you can just swap the two bottom lines makingremoveCard(i)
the last.Demo:
Consider the following.
This is an example that is a bit more complex, yet the same basic apply.
This pulls a Card object from the top of the deck (using
.pop()
). A new<img>
element is created with specific attributes. It is appended to the document and made draggable.The User can now drag it. Droppable must be configured correctly too.