0

New to javascript, but in my mind my html should produce a grid that I created. None of the tr or td elements are being populated from my js file. Does anyone know why or what I'm missing? Thanks for your help.

Here is my js file:

function showGrid() {
    var gridDiv = document.getElementById("gridDiv");
    gridDiv.innerHTML = genGrid();
}

function genGrid() {
    var html = "";
    var row = 8;
    var i = 0;
    var j = 0;
    var tdClass = "";

    for (i = row; i > 0; i--) {

        html += "<tr>";

        for (j = row; j > 0; j--) {
            var r = j % 2;
            if (r = 0) {
                tdClass = "red";
            } else {
                tdClass = "black";
            }

            html += "<td class=\"" + tdClass + "\"></td>";
        }
        html += "</tr>";
    }
    return html;
}

Here is my html file:

<!DOCTYPE HTML>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">

<html>
    <head>
        <link rel="stylesheet" type=text/css href="style.css">
        <script src="genGame.js"></script>
    </head>
    <body onload="showGrid()">

        <div id="gridDiv">
        This should get overridden
        </div>
    </body>

</html>
4
  • 1
    You are missing data. There's nothing in your td elements. Hence there's nothing to show. Commented Feb 1, 2015 at 7:42
  • That depends on the CSS Commented Feb 1, 2015 at 7:44
  • If I had to guess, the OP probably has width, height, and background CSS that is not posted Commented Feb 1, 2015 at 7:44
  • 1
    You also have a typo. "if (r = 0) {" should be "if (r === 0) {" as you have it, it is assgning 0 to r, but tests like "if(0)" which is false so always will be black Commented Feb 1, 2015 at 8:46

2 Answers 2

1

This

    <div id="gridDiv">
    This should get overridden
    </div>

Should be

    <table id="gridDiv">
    This should get overridden
    </table>
Sign up to request clarification or add additional context in comments.

3 Comments

That's not the only problem. The problem is that there's nothing in the OP's table.
html += "<td class=\"" + tdClass + "\"></td>";
this made the table appear in the html, ty @Ed Heal.
0

You have no data in <td></td>

<script>
    function showGrid() {
   var gridDiv = document.getElementById("gridDiv");
    gridDiv.innerHTML = genGrid();
}
    function genGrid() {
    var html = "";
    var row = 8;
    var i = 0;
    var j = 0;
    var tdClass = "";

    for (i = row; i > 0; i--) {

        html += "<tr>";

        for (j = row; j > 0; j--) {
            var r = j % 2;
            if (r == 0) {
                tdClass = "red";
            } else {
                tdClass = "black";
            }

            html += "<td class=\"" + tdClass + "\">-data-</td>";
        }
        html += "</tr>";
    }
    return html;
}
</script>



<body onload="showGrid()">
        <table id="gridDiv">
            This should get overridden
        </table>
 </body>

comparision should also ok

if (r == 0) {
                tdClass = "red";
            }

link

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.