1

In this block of code I am loading some images of dice, and trying to display a few of those in the table. I am not sure if I am even building the array right, but this was one of the examples I found of propagating an array with images.

<table id="dicebox">
                <tr>
                    <td id="d1"></td>
                    <td id="d2"></td>
                    <td id="d3"></td>
                    <td id="d4"></td>
                    <td id="d5"></td>
                </tr>
            </table>
<script>

            var diceB = new Array(7);
            //Blue dice
            var diceP = new Array(7);
            //Purple dice
            var diceValue = new Array(0, 1, 2, 3, 4, 5, 6);

            //initializing blue dice array
            diceB[0] = new Image(50, 50);
            diceB[0].src = "images/dice0B.gif"
            diceB[1] = new Image(50, 50);
            diceB[1].src = "images/dice1B.gif"
            diceB[2] = new Image(50, 50);
            diceB[2].src = "images/dice2B.gif"
            diceB[3] = new Image(50, 50);
            diceB[3].src = "images/dice3B.gif"
            diceB[4] = new Image(50, 50);
            diceB[4].src = "images/dice4B.gif"
            diceB[5] = new Image(50, 50);
            diceB[5].src = "images/dice5B.gif"
            diceB[6] = new Image(50, 50);
            diceB[6].src = "images/dice6B.gif"


            for(var i = 1; i < 6; i++) {
                document.getElementById('d' + i).innerHTML = diceB[i].src;
2
  • What do you mean by "dice"? Commented Dec 4, 2012 at 21:38
  • 1
    The things you roll in games Commented Dec 4, 2012 at 23:37

2 Answers 2

1

How about using appendChild

for(var i = 1; i < 6; i++) {
    document.getElementById('d' + i).appendChild(diceB[i]);
Sign up to request clarification or add additional context in comments.

2 Comments

Can you append an Image object directly or do you need to create an image tag?
@LeviBotelho Image tags is used in html mark up, image object is used in the DOM
1

You did not write an image tag, you have to do the following:

for(var i = 1; i < 6; i++) {
                document.getElementById('d' + i).innerHTML = '<img src="'+diceB[i].src+'" />';

}

Comments

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.