1

So I have:

arrayofobj = [
    {
        property: "string",   // "card" object with different data types
        property2: integer
    },
    {
        property: "string",   // "card" object with different data types
        property2: integer
    },
    {
        //etc more cards
    }
];

var playDeck = arrayofobj.slice(0);//creates copy of deck to "use" for game
shuffled(playDeck);//shuffles deck
playerHand = [];//empty array to save cards into
deal10(playDeck);//removes the 1st 10 out of playDeck and puts in playerHand

Now from the 10 that get drawn into playerHand, I'm trying to display them on screen. I have a field set up with:

<div id="playerHand"></div>

My attempt which hasn't been very successful:

for (var i = 0; i < playerHand.length; i++) {
    content = playerHand[i];
    x = document.getElementById('playerHand').appendChild(document.createElement('div'));
    x.className = 'card' + [i];
    document.getElementsByClassName('card' + [i]).textContent = content;
}

I haven't been able to achieve the desired effect. I'm a beginner as far as programming goes so I'm open to constructive criticism. So if you haven't gathered already what I'm trying to do is display each object or card on the screen as its own div (so that I can add click handlers to them and have the clicked one played on the screen... for example.)

3
  • Shouldn't you be using player1Hand as ID instead of playerHand then? in your HTML? Commented Oct 9, 2015 at 6:25
  • document.getElementById('player1Hand').. Is this ID name correct? Commented Oct 9, 2015 at 6:25
  • it is -.- just changed it for readability... sorry :( Commented Oct 9, 2015 at 6:26

1 Answer 1

2

I think the main problem is getElementsByClassName, which returns an array and not the actual reference to the element. See below:

var data, content, container, element;

container = document.getElementById('container');
data = [
    {
        name: "stringa"
    },
    {
        name: "stringb"
    }
];


for (var i = 0; i < data.length; i++) {
    // Are you accessing an actual property in your code? It seems
    // you are just referencing the whole object.
    content = data[ i ].name;

    // Just save the createElement reference ;)
    element = document.createElement('div');

    // Why are you appending [i] instead of i directly?
    element.className = 'card' + i;

    // By saving the element directly we don't have to query for
    // the class in this step. The other problem was probably that
    // `getElementsByClassName` returns an array, so you would have
    // to call:                                      vvv
    // document.getElementsByClassName('card' + [i])[ 0 ]
    element.textContent = content;

    container.appendChild( element );
}

Obligatory JSFiddle: http://jsfiddle.net/tgtefsrm/1/

Sign up to request clarification or add additional context in comments.

2 Comments

<3 you're a legend. that worked perfectly. as the whole object and with properties. Thanks a bunch!!
Glad you are happy :-) Don't forget to mark the correct answer on your questions!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.