Changing td element text using Javascript

I am writing code for html file that will display the number of each sum of dice for 36000 thrown dices. I wrote code and everything seems fine, I printed the numbers of each sum and I got numbers, but when I try to put them inside the td element, I see nothing in the table. I searched on stackoverflow and on google and all said same thing, but it's not working. Here is my code:

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script type="text/javascript">
            <!--

            /*

                Position #0 : Sum of Dices = 2
                Position #1 : Sum of Dices = 3
                Position #2 : Sum of Dices = 4
                Position #3 : Sum of Dices = 5
                Position #4 : Sum of Dices = 6
                Position #5 : Sum of Dices = 7
                Position #6 : Sum of Dices = 8
                Position #7 : Sum of Dices = 9
                Position #8 : Sum of Dices = 10
                Position #9 : Sum of Dices = 11
                Position #10: Sum of Dices = 12

            */
            var sums = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

            for (var i = 0; i < 36000; i++) {
                var dice1 = Math.floor(Math.random() * 6) + 1;
                var dice2 = Math.floor(Math.random() * 6) + 1;
                var sum = dice1 + dice2;

                switch(sum) {
                    case 2:
                        sums[0]++;
                        break;
                    case 3:
                        sums[1]++;
                        break;
                    case 4:
                        sums[2]++;
                        break;
                    case 5:
                        sums[3]++;
                        break;
                    case 6:
                        sums[4]++;
                        break;
                    case 7:
                        sums[5]++;
                        break;
                    case 8:
                        sums[6]++;
                        break;
                    case 9:
                        sums[7]++;
                        break;
                    case 10:
                        sums[8]++;
                        break;
                    case 11:
                        sums[9]++;
                        break;
                    case 12:
                        sums[10]++;
                        break;
                    default:
                        break;
                }
            }

            document.getElementById("sum2").innerHTML = sums[0];
            document.getElementById("sum3").innerHTML = sums[1];
            document.getElementById("sum4").innerHTML = sums[2];
            document.getElementById("sum5").innerHTML = sums[3];
            document.getElementById("sum6").innerHTML = sums[4];
            document.getElementById("sum7").innerHTML = sums[5];
            document.getElementById("sum8").innerHTML = sums[6];
            document.getElementById("sum9").innerHTML = sums[7];
            document.getElementById("sum10").innerHTML = sums[8];
            document.getElementById("sum11").innerHTML = sums[9];
            document.getElementById("sum12").innerHTML = sums[10];

            // -->
        </script>
    </head>

    <body>

        <h1>Roll Dice 36,000 Times</h1>

        <table border="1">
            <th>Sum of Dice</th>
            <th>Total Times Rolled</th>

            <tr>
                <td>2</td>
                <td id="sum2"></td>
            </tr>
            <tr>
                <td>3</td>
                <td id="sum3"></td>
            </tr>
            <tr>
                <td>4</td>
                <td id="sum4"></td>
            </tr>
            <tr>
                <td>5</td>
                <td id="sum5"></td>
            </tr>
            <tr>
                <td>6</td>
                <td id="sum6"></td>
            </tr>
            <tr>
                <td>7</td>
                <td id="sum7"></td>
            </tr>
            <tr>
                <td>8</td>
                <td id="sum8"></td>
            </tr>
            <tr>
                <td>9</td>
                <td id="sum9"></td>
            </tr>
            <tr>
                <td>10</td>
                <td id="sum10"></td>
            </tr>
            <tr>
                <td>11</td>
                <td id="sum11"></td>
            </tr>
            <tr>
                <td>12</td>
                <td id="sum12"></td>
            </tr>
        </table>
    </body>
</html>

Some said to use innerText, and textContent but none seem to work. Why is it not working?

I don't know if this helps but I'm using Google Chrome on Mac OS X

Answers:

Answer

Use windows.onload function to be sure html is loaded:

window.onload=function(){

        document.getElementById("sum2").innerHTML = sums[0];
        document.getElementById("sum3").innerHTML = sums[1];
        document.getElementById("sum4").innerHTML = sums[2];
        document.getElementById("sum5").innerHTML = sums[3];
        document.getElementById("sum6").innerHTML = sums[4];
        document.getElementById("sum7").innerHTML = sums[5];
        document.getElementById("sum8").innerHTML = sums[6];
        document.getElementById("sum9").innerHTML = sums[7];
        document.getElementById("sum10").innerHTML = sums[8];
        document.getElementById("sum11").innerHTML = sums[9];
        document.getElementById("sum12").innerHTML = sums[10];
};
Answer

Your JS runs before the DOM was ready. Put your script within window.onload and try again,

    <script type="text/javascript">
    <!--
       window.onload = function(){

                /*

                    Position #0 : Sum of Dices = 2
                    Position #1 : Sum of Dices = 3
                    Position #2 : Sum of Dices = 4
                    Position #3 : Sum of Dices = 5
                    Position #4 : Sum of Dices = 6
                    Position #5 : Sum of Dices = 7
                    Position #6 : Sum of Dices = 8
                    Position #7 : Sum of Dices = 9
                    Position #8 : Sum of Dices = 10
                    Position #9 : Sum of Dices = 11
                    Position #10: Sum of Dices = 12

                */
                var sums = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

                for (var i = 0; i < 36000; i++) {
                    var dice1 = Math.floor(Math.random() * 6) + 1;
                    var dice2 = Math.floor(Math.random() * 6) + 1;
                    var sum = dice1 + dice2;

                    switch(sum) {
                        case 2:
                            sums[0]++;
                            break;
                        case 3:
                            sums[1]++;
                            break;
                        case 4:
                            sums[2]++;
                            break;
                        case 5:
                            sums[3]++;
                            break;
                        case 6:
                            sums[4]++;
                            break;
                        case 7:
                            sums[5]++;
                            break;
                        case 8:
                            sums[6]++;
                            break;
                        case 9:
                            sums[7]++;
                            break;
                        case 10:
                            sums[8]++;
                            break;
                        case 11:
                            sums[9]++;
                            break;
                        case 12:
                            sums[10]++;
                            break;
                        default:
                            break;
                    }
                }

                document.getElementById("sum2").innerHTML = sums[0];
                document.getElementById("sum3").innerHTML = sums[1];
                document.getElementById("sum4").innerHTML = sums[2];
                document.getElementById("sum5").innerHTML = sums[3];
                document.getElementById("sum6").innerHTML = sums[4];
                document.getElementById("sum7").innerHTML = sums[5];
                document.getElementById("sum8").innerHTML = sums[6];
                document.getElementById("sum9").innerHTML = sums[7];
                document.getElementById("sum10").innerHTML = sums[8];
                document.getElementById("sum11").innerHTML = sums[9];
                document.getElementById("sum12").innerHTML = sums[10];

      }
   // -->
   </script>
Answer

Try this:

$(function(){
    var sums = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

            for (var i = 0; i < 36000; i++) {
                var dice1 = Math.floor(Math.random() * 6) + 1;
                var dice2 = Math.floor(Math.random() * 6) + 1;
                var sum = dice1 + dice2;

                switch(sum) {
                    case 2:
                        sums[0]++;
                        break;
                    case 3:
                        sums[1]++;
                        break;
                    case 4:
                        sums[2]++;
                        break;
                    case 5:
                        sums[3]++;
                        break;
                    case 6:
                        sums[4]++;
                        break;
                    case 7:
                        sums[5]++;
                        break;
                    case 8:
                        sums[6]++;
                        break;
                    case 9:
                        sums[7]++;
                        break;
                    case 10:
                        sums[8]++;
                        break;
                    case 11:
                        sums[9]++;
                        break;
                    case 12:
                        sums[10]++;
                        break;
                    default:
                        break;
                }
            }

            document.getElementById("sum2").innerHTML = sums[0];
            document.getElementById("sum3").innerHTML = sums[1];
            document.getElementById("sum4").innerHTML = sums[2];
            document.getElementById("sum5").innerHTML = sums[3];
            document.getElementById("sum6").innerHTML = sums[4];
            document.getElementById("sum7").innerHTML = sums[5];
            document.getElementById("sum8").innerHTML = sums[6];
            document.getElementById("sum9").innerHTML = sums[7];
            document.getElementById("sum10").innerHTML = sums[8];
            document.getElementById("sum11").innerHTML = sums[9];
            document.getElementById("sum12").innerHTML = sums[10];
});
Answer

Try something like

for(var i = 0; i <sums.length; i++){
    document.getElementById("sum" + (i + 2)).innerHTML = sums[i];
}

Demo: Fiddle

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.