Chessboard in pure js












15















I began js 3 days ago at school, I had some C basics before so the main problem here would be the syntax (I think).



The goal is to produce a chessboard, 8x8 black and white square, but I can't get the code to show anything. What am I missing ?



(the html just have the script src="./x.js" part and the "body" part)



document.body.onload = addElement.innerHTML;

function addElement() {

var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}

document.body.appendChild(newTable);

document.getElementByClass("black").style.backgroundColor = "black";
document.getElementByClass("white").style.backgroundColor = "white";
document.getElementByTag("newTd").style.width = "25px";
document.getElementByTag("newTd").style.height = "25px";
}









share|improve this question




















  • 13





    Quick tip - press F12 and you'll be able to see any errors in your console.

    – markmoxx
    Dec 12 '18 at 10:56
















15















I began js 3 days ago at school, I had some C basics before so the main problem here would be the syntax (I think).



The goal is to produce a chessboard, 8x8 black and white square, but I can't get the code to show anything. What am I missing ?



(the html just have the script src="./x.js" part and the "body" part)



document.body.onload = addElement.innerHTML;

function addElement() {

var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}

document.body.appendChild(newTable);

document.getElementByClass("black").style.backgroundColor = "black";
document.getElementByClass("white").style.backgroundColor = "white";
document.getElementByTag("newTd").style.width = "25px";
document.getElementByTag("newTd").style.height = "25px";
}









share|improve this question




















  • 13





    Quick tip - press F12 and you'll be able to see any errors in your console.

    – markmoxx
    Dec 12 '18 at 10:56














15












15








15








I began js 3 days ago at school, I had some C basics before so the main problem here would be the syntax (I think).



The goal is to produce a chessboard, 8x8 black and white square, but I can't get the code to show anything. What am I missing ?



(the html just have the script src="./x.js" part and the "body" part)



document.body.onload = addElement.innerHTML;

function addElement() {

var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}

document.body.appendChild(newTable);

document.getElementByClass("black").style.backgroundColor = "black";
document.getElementByClass("white").style.backgroundColor = "white";
document.getElementByTag("newTd").style.width = "25px";
document.getElementByTag("newTd").style.height = "25px";
}









share|improve this question
















I began js 3 days ago at school, I had some C basics before so the main problem here would be the syntax (I think).



The goal is to produce a chessboard, 8x8 black and white square, but I can't get the code to show anything. What am I missing ?



(the html just have the script src="./x.js" part and the "body" part)



document.body.onload = addElement.innerHTML;

function addElement() {

var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}

document.body.appendChild(newTable);

document.getElementByClass("black").style.backgroundColor = "black";
document.getElementByClass("white").style.backgroundColor = "white";
document.getElementByTag("newTd").style.width = "25px";
document.getElementByTag("newTd").style.height = "25px";
}






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 12 '18 at 11:13









markmoxx

1,147617




1,147617










asked Dec 12 '18 at 10:45









JauneJaune

907




907








  • 13





    Quick tip - press F12 and you'll be able to see any errors in your console.

    – markmoxx
    Dec 12 '18 at 10:56














  • 13





    Quick tip - press F12 and you'll be able to see any errors in your console.

    – markmoxx
    Dec 12 '18 at 10:56








13




13





Quick tip - press F12 and you'll be able to see any errors in your console.

– markmoxx
Dec 12 '18 at 10:56





Quick tip - press F12 and you'll be able to see any errors in your console.

– markmoxx
Dec 12 '18 at 10:56












5 Answers
5






active

oldest

votes


















12














There are few mistakes in your code use getElementsByClassName instead of getElementByClass and use getElementsByTagName instead of getElementByTag.



Also you need to loop over each selected elements.



Use window.onload = addElement; or you can simply call addElement(); after function declaration completes.






window.onload = addElement;

function addElement() {
var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}

document.body.appendChild(newTable);
var i = 0;
for (i = 0; i < document.getElementsByClassName("black").length; i++) {
document.getElementsByClassName("black")[i].style.backgroundColor = "black";
}
for (i = 0; i < document.getElementsByClassName("white").length; i++) {
document.getElementsByClassName("white")[i].style.backgroundColor = "white";
}
for (i = 0; i < document.getElementsByTagName("td").length; i++) {
document.getElementsByTagName("td")[i].style.width = "25px";
document.getElementsByTagName("td")[i].style.height = "25px";
}

}








share|improve this answer































    5














    A simpler approach would be to add the attributes when you create each element and not after. Like this:



    if (i % 2 == j % 2) newTd.style.backgroundColor = "white";

    else newTd.style.backgroundColor = "black";

    newTd.style.width = "25px";

    newTd.style.height = "25px";


    See this fiddle: http://jsfiddle.net/vdquLn65/2/



    About your current code you could use getElementsByClassName and getElementsByTagName and iterate over the elements they return to do what you want.






    share|improve this answer
























    • Yeah I thought about that too while making the tests, but I opted in the current organisation so the css is all in the same spot, after the main js part. It could have made it easier to solve my problem tho, or even solve it. Thanks for the advice

      – Jaune
      Dec 12 '18 at 11:16



















    4














    As you currently have it, addElement.innerHTML is a reference to the innerHTML property of the addElement variable. addElement is a reference to a function definition, and does not have an innerHTML property.



    Try changing your first line to this:



    window.onload = addElement;


    See: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload



    Edit: I was a bit too quick to post this, and didn't spot the other errors in the code as mentioned in Karan's answer.






    share|improve this answer





















    • 1





      There is no explanation what so ever, he is curious about knowing what he did wrong, the solution might be useful, but seems less of a priority / necessity

      – Icepickle
      Dec 12 '18 at 10:51











    • Oops, thanks @RolandStarke

      – markmoxx
      Dec 12 '18 at 10:52











    • Well indeed it doesn't solve my problem, but I'm learning so I'm still happy to understand something better so thank anyway

      – Jaune
      Dec 12 '18 at 10:56



















    2














    Hello Jaune and welcome to Stack Overflow. I have added an answer that will create the data first and then map that that data to elements using functions. If you have any questions please let me know.






    const field = ([x, y, color]) => {
    const td = document.createElement('td');
    td.setAttribute('data-x', x);
    td.setAttribute('data-y', y);
    td.className = color;
    td.innerHtml = `${x}-${y}`;
    return td;
    };
    const row = (row) => {
    const tr = document.createElement('tr');
    row.forEach((fieldItem) =>
    tr.appendChild(field(fieldItem)),
    );
    return tr;
    };
    const table = (tableData) => {
    const table = document.createElement('table');
    tableData.forEach((rowData) =>
    table.appendChild(row(rowData)),
    );
    return table;
    };
    const tableData = Array.from(Array(8).keys()).map((x) =>
    Array.from(Array(8).keys()).map((y) => [
    x,
    y,
    (x + y) % 2 ? 'black' : 'white',
    ]),
    );
    document.body.appendChild(table(tableData));

    .black {
    width:10px;
    height:10px;
    background-color: black;
    }
    .white {
    width:10px;
    height:10px;
    background-color: white;
    }
    table {
    border: 1px solid black;
    }








    share|improve this answer
























    • Hello HMR, and thanks for the warm welcome. I'd gladly take your proposal for help, but I'm still a little too fresh with the syntax to have interesting question about your code at the moment. Thanks again tho

      – Jaune
      Dec 12 '18 at 11:28






    • 1





      @Jaune The following book can help you out with syntax and here are some handy array functions.

      – HMR
      Dec 12 '18 at 11:53






    • 1





      Great docs, thanks a lot !

      – Jaune
      Dec 12 '18 at 12:47











    • While this is certainly the preferable way to do it, I'd like to point out that the question was to produce a chessboard using Javascript alone.

      – Abion47
      Dec 12 '18 at 19:06



















    1














    As an alternative approach, consider coloring the table itself black and only coloring the corresponding tiles white:






    window.onload = addElement;

    function addElement() {
    var tbl = document.createElement('table');
    tbl.style.backgroundColor = 'black';

    for (var i = 1; i < 9; i++) {
    var tr = document.createElement('tr');

    for (var j = 1; j < 9; j++) {
    var td = document.createElement('td');
    td.style.width = '25px';
    td.style.height = '25px';

    if (i % 2 == j % 2) {
    td.style.backgroundColor = 'white';
    }

    tr.appendChild(td);
    }

    tbl.appendChild(tr);
    }

    document.body.appendChild(tbl);
    }





    And also, as Alex G said, it's much simpler to do the styling at the time the element is created instead of trying to iterate over them later. I understand the mindset of separating the logic and style portions of the code, but in this case it isn't really worth the added effort and complexity, and there are better ways to do it anyway.



    And besides, if you wanted to separate the style from the logic, that's what CSS files are for. :P






    share|improve this answer
























    • I know I know, but the exercise being JS only, I didn't even had a .css or a <style> soooo it kinda messed my head up tbh :p Another student indeed went with the white td on black table thanks to your advice and it went well !

      – Jaune
      Dec 13 '18 at 12:57













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53741270%2fchessboard-in-pure-js%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    12














    There are few mistakes in your code use getElementsByClassName instead of getElementByClass and use getElementsByTagName instead of getElementByTag.



    Also you need to loop over each selected elements.



    Use window.onload = addElement; or you can simply call addElement(); after function declaration completes.






    window.onload = addElement;

    function addElement() {
    var newTable = document.createElement("table");
    for (var i = 1; i < 9; i++) {
    var newTr = document.createElement('tr');
    for (var j = 1; j < 9; j++) {
    var newTd = document.createElement('td');
    if (i % 2 == j % 2) {
    newTd.className = "white";
    } else {
    newTd.className = "black";
    }
    newTr.appendChild(newTd);
    }
    newTable.appendChild(newTr);
    }

    document.body.appendChild(newTable);
    var i = 0;
    for (i = 0; i < document.getElementsByClassName("black").length; i++) {
    document.getElementsByClassName("black")[i].style.backgroundColor = "black";
    }
    for (i = 0; i < document.getElementsByClassName("white").length; i++) {
    document.getElementsByClassName("white")[i].style.backgroundColor = "white";
    }
    for (i = 0; i < document.getElementsByTagName("td").length; i++) {
    document.getElementsByTagName("td")[i].style.width = "25px";
    document.getElementsByTagName("td")[i].style.height = "25px";
    }

    }








    share|improve this answer




























      12














      There are few mistakes in your code use getElementsByClassName instead of getElementByClass and use getElementsByTagName instead of getElementByTag.



      Also you need to loop over each selected elements.



      Use window.onload = addElement; or you can simply call addElement(); after function declaration completes.






      window.onload = addElement;

      function addElement() {
      var newTable = document.createElement("table");
      for (var i = 1; i < 9; i++) {
      var newTr = document.createElement('tr');
      for (var j = 1; j < 9; j++) {
      var newTd = document.createElement('td');
      if (i % 2 == j % 2) {
      newTd.className = "white";
      } else {
      newTd.className = "black";
      }
      newTr.appendChild(newTd);
      }
      newTable.appendChild(newTr);
      }

      document.body.appendChild(newTable);
      var i = 0;
      for (i = 0; i < document.getElementsByClassName("black").length; i++) {
      document.getElementsByClassName("black")[i].style.backgroundColor = "black";
      }
      for (i = 0; i < document.getElementsByClassName("white").length; i++) {
      document.getElementsByClassName("white")[i].style.backgroundColor = "white";
      }
      for (i = 0; i < document.getElementsByTagName("td").length; i++) {
      document.getElementsByTagName("td")[i].style.width = "25px";
      document.getElementsByTagName("td")[i].style.height = "25px";
      }

      }








      share|improve this answer


























        12












        12








        12







        There are few mistakes in your code use getElementsByClassName instead of getElementByClass and use getElementsByTagName instead of getElementByTag.



        Also you need to loop over each selected elements.



        Use window.onload = addElement; or you can simply call addElement(); after function declaration completes.






        window.onload = addElement;

        function addElement() {
        var newTable = document.createElement("table");
        for (var i = 1; i < 9; i++) {
        var newTr = document.createElement('tr');
        for (var j = 1; j < 9; j++) {
        var newTd = document.createElement('td');
        if (i % 2 == j % 2) {
        newTd.className = "white";
        } else {
        newTd.className = "black";
        }
        newTr.appendChild(newTd);
        }
        newTable.appendChild(newTr);
        }

        document.body.appendChild(newTable);
        var i = 0;
        for (i = 0; i < document.getElementsByClassName("black").length; i++) {
        document.getElementsByClassName("black")[i].style.backgroundColor = "black";
        }
        for (i = 0; i < document.getElementsByClassName("white").length; i++) {
        document.getElementsByClassName("white")[i].style.backgroundColor = "white";
        }
        for (i = 0; i < document.getElementsByTagName("td").length; i++) {
        document.getElementsByTagName("td")[i].style.width = "25px";
        document.getElementsByTagName("td")[i].style.height = "25px";
        }

        }








        share|improve this answer













        There are few mistakes in your code use getElementsByClassName instead of getElementByClass and use getElementsByTagName instead of getElementByTag.



        Also you need to loop over each selected elements.



        Use window.onload = addElement; or you can simply call addElement(); after function declaration completes.






        window.onload = addElement;

        function addElement() {
        var newTable = document.createElement("table");
        for (var i = 1; i < 9; i++) {
        var newTr = document.createElement('tr');
        for (var j = 1; j < 9; j++) {
        var newTd = document.createElement('td');
        if (i % 2 == j % 2) {
        newTd.className = "white";
        } else {
        newTd.className = "black";
        }
        newTr.appendChild(newTd);
        }
        newTable.appendChild(newTr);
        }

        document.body.appendChild(newTable);
        var i = 0;
        for (i = 0; i < document.getElementsByClassName("black").length; i++) {
        document.getElementsByClassName("black")[i].style.backgroundColor = "black";
        }
        for (i = 0; i < document.getElementsByClassName("white").length; i++) {
        document.getElementsByClassName("white")[i].style.backgroundColor = "white";
        }
        for (i = 0; i < document.getElementsByTagName("td").length; i++) {
        document.getElementsByTagName("td")[i].style.width = "25px";
        document.getElementsByTagName("td")[i].style.height = "25px";
        }

        }








        window.onload = addElement;

        function addElement() {
        var newTable = document.createElement("table");
        for (var i = 1; i < 9; i++) {
        var newTr = document.createElement('tr');
        for (var j = 1; j < 9; j++) {
        var newTd = document.createElement('td');
        if (i % 2 == j % 2) {
        newTd.className = "white";
        } else {
        newTd.className = "black";
        }
        newTr.appendChild(newTd);
        }
        newTable.appendChild(newTr);
        }

        document.body.appendChild(newTable);
        var i = 0;
        for (i = 0; i < document.getElementsByClassName("black").length; i++) {
        document.getElementsByClassName("black")[i].style.backgroundColor = "black";
        }
        for (i = 0; i < document.getElementsByClassName("white").length; i++) {
        document.getElementsByClassName("white")[i].style.backgroundColor = "white";
        }
        for (i = 0; i < document.getElementsByTagName("td").length; i++) {
        document.getElementsByTagName("td")[i].style.width = "25px";
        document.getElementsByTagName("td")[i].style.height = "25px";
        }

        }





        window.onload = addElement;

        function addElement() {
        var newTable = document.createElement("table");
        for (var i = 1; i < 9; i++) {
        var newTr = document.createElement('tr');
        for (var j = 1; j < 9; j++) {
        var newTd = document.createElement('td');
        if (i % 2 == j % 2) {
        newTd.className = "white";
        } else {
        newTd.className = "black";
        }
        newTr.appendChild(newTd);
        }
        newTable.appendChild(newTr);
        }

        document.body.appendChild(newTable);
        var i = 0;
        for (i = 0; i < document.getElementsByClassName("black").length; i++) {
        document.getElementsByClassName("black")[i].style.backgroundColor = "black";
        }
        for (i = 0; i < document.getElementsByClassName("white").length; i++) {
        document.getElementsByClassName("white")[i].style.backgroundColor = "white";
        }
        for (i = 0; i < document.getElementsByTagName("td").length; i++) {
        document.getElementsByTagName("td")[i].style.width = "25px";
        document.getElementsByTagName("td")[i].style.height = "25px";
        }

        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 12 '18 at 10:54









        KaranKaran

        3,3762424




        3,3762424

























            5














            A simpler approach would be to add the attributes when you create each element and not after. Like this:



            if (i % 2 == j % 2) newTd.style.backgroundColor = "white";

            else newTd.style.backgroundColor = "black";

            newTd.style.width = "25px";

            newTd.style.height = "25px";


            See this fiddle: http://jsfiddle.net/vdquLn65/2/



            About your current code you could use getElementsByClassName and getElementsByTagName and iterate over the elements they return to do what you want.






            share|improve this answer
























            • Yeah I thought about that too while making the tests, but I opted in the current organisation so the css is all in the same spot, after the main js part. It could have made it easier to solve my problem tho, or even solve it. Thanks for the advice

              – Jaune
              Dec 12 '18 at 11:16
















            5














            A simpler approach would be to add the attributes when you create each element and not after. Like this:



            if (i % 2 == j % 2) newTd.style.backgroundColor = "white";

            else newTd.style.backgroundColor = "black";

            newTd.style.width = "25px";

            newTd.style.height = "25px";


            See this fiddle: http://jsfiddle.net/vdquLn65/2/



            About your current code you could use getElementsByClassName and getElementsByTagName and iterate over the elements they return to do what you want.






            share|improve this answer
























            • Yeah I thought about that too while making the tests, but I opted in the current organisation so the css is all in the same spot, after the main js part. It could have made it easier to solve my problem tho, or even solve it. Thanks for the advice

              – Jaune
              Dec 12 '18 at 11:16














            5












            5








            5







            A simpler approach would be to add the attributes when you create each element and not after. Like this:



            if (i % 2 == j % 2) newTd.style.backgroundColor = "white";

            else newTd.style.backgroundColor = "black";

            newTd.style.width = "25px";

            newTd.style.height = "25px";


            See this fiddle: http://jsfiddle.net/vdquLn65/2/



            About your current code you could use getElementsByClassName and getElementsByTagName and iterate over the elements they return to do what you want.






            share|improve this answer













            A simpler approach would be to add the attributes when you create each element and not after. Like this:



            if (i % 2 == j % 2) newTd.style.backgroundColor = "white";

            else newTd.style.backgroundColor = "black";

            newTd.style.width = "25px";

            newTd.style.height = "25px";


            See this fiddle: http://jsfiddle.net/vdquLn65/2/



            About your current code you could use getElementsByClassName and getElementsByTagName and iterate over the elements they return to do what you want.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 12 '18 at 11:04









            Alex GAlex G

            1,289139




            1,289139













            • Yeah I thought about that too while making the tests, but I opted in the current organisation so the css is all in the same spot, after the main js part. It could have made it easier to solve my problem tho, or even solve it. Thanks for the advice

              – Jaune
              Dec 12 '18 at 11:16



















            • Yeah I thought about that too while making the tests, but I opted in the current organisation so the css is all in the same spot, after the main js part. It could have made it easier to solve my problem tho, or even solve it. Thanks for the advice

              – Jaune
              Dec 12 '18 at 11:16

















            Yeah I thought about that too while making the tests, but I opted in the current organisation so the css is all in the same spot, after the main js part. It could have made it easier to solve my problem tho, or even solve it. Thanks for the advice

            – Jaune
            Dec 12 '18 at 11:16





            Yeah I thought about that too while making the tests, but I opted in the current organisation so the css is all in the same spot, after the main js part. It could have made it easier to solve my problem tho, or even solve it. Thanks for the advice

            – Jaune
            Dec 12 '18 at 11:16











            4














            As you currently have it, addElement.innerHTML is a reference to the innerHTML property of the addElement variable. addElement is a reference to a function definition, and does not have an innerHTML property.



            Try changing your first line to this:



            window.onload = addElement;


            See: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload



            Edit: I was a bit too quick to post this, and didn't spot the other errors in the code as mentioned in Karan's answer.






            share|improve this answer





















            • 1





              There is no explanation what so ever, he is curious about knowing what he did wrong, the solution might be useful, but seems less of a priority / necessity

              – Icepickle
              Dec 12 '18 at 10:51











            • Oops, thanks @RolandStarke

              – markmoxx
              Dec 12 '18 at 10:52











            • Well indeed it doesn't solve my problem, but I'm learning so I'm still happy to understand something better so thank anyway

              – Jaune
              Dec 12 '18 at 10:56
















            4














            As you currently have it, addElement.innerHTML is a reference to the innerHTML property of the addElement variable. addElement is a reference to a function definition, and does not have an innerHTML property.



            Try changing your first line to this:



            window.onload = addElement;


            See: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload



            Edit: I was a bit too quick to post this, and didn't spot the other errors in the code as mentioned in Karan's answer.






            share|improve this answer





















            • 1





              There is no explanation what so ever, he is curious about knowing what he did wrong, the solution might be useful, but seems less of a priority / necessity

              – Icepickle
              Dec 12 '18 at 10:51











            • Oops, thanks @RolandStarke

              – markmoxx
              Dec 12 '18 at 10:52











            • Well indeed it doesn't solve my problem, but I'm learning so I'm still happy to understand something better so thank anyway

              – Jaune
              Dec 12 '18 at 10:56














            4












            4








            4







            As you currently have it, addElement.innerHTML is a reference to the innerHTML property of the addElement variable. addElement is a reference to a function definition, and does not have an innerHTML property.



            Try changing your first line to this:



            window.onload = addElement;


            See: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload



            Edit: I was a bit too quick to post this, and didn't spot the other errors in the code as mentioned in Karan's answer.






            share|improve this answer















            As you currently have it, addElement.innerHTML is a reference to the innerHTML property of the addElement variable. addElement is a reference to a function definition, and does not have an innerHTML property.



            Try changing your first line to this:



            window.onload = addElement;


            See: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload



            Edit: I was a bit too quick to post this, and didn't spot the other errors in the code as mentioned in Karan's answer.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 12 '18 at 10:58

























            answered Dec 12 '18 at 10:49









            markmoxxmarkmoxx

            1,147617




            1,147617








            • 1





              There is no explanation what so ever, he is curious about knowing what he did wrong, the solution might be useful, but seems less of a priority / necessity

              – Icepickle
              Dec 12 '18 at 10:51











            • Oops, thanks @RolandStarke

              – markmoxx
              Dec 12 '18 at 10:52











            • Well indeed it doesn't solve my problem, but I'm learning so I'm still happy to understand something better so thank anyway

              – Jaune
              Dec 12 '18 at 10:56














            • 1





              There is no explanation what so ever, he is curious about knowing what he did wrong, the solution might be useful, but seems less of a priority / necessity

              – Icepickle
              Dec 12 '18 at 10:51











            • Oops, thanks @RolandStarke

              – markmoxx
              Dec 12 '18 at 10:52











            • Well indeed it doesn't solve my problem, but I'm learning so I'm still happy to understand something better so thank anyway

              – Jaune
              Dec 12 '18 at 10:56








            1




            1





            There is no explanation what so ever, he is curious about knowing what he did wrong, the solution might be useful, but seems less of a priority / necessity

            – Icepickle
            Dec 12 '18 at 10:51





            There is no explanation what so ever, he is curious about knowing what he did wrong, the solution might be useful, but seems less of a priority / necessity

            – Icepickle
            Dec 12 '18 at 10:51













            Oops, thanks @RolandStarke

            – markmoxx
            Dec 12 '18 at 10:52





            Oops, thanks @RolandStarke

            – markmoxx
            Dec 12 '18 at 10:52













            Well indeed it doesn't solve my problem, but I'm learning so I'm still happy to understand something better so thank anyway

            – Jaune
            Dec 12 '18 at 10:56





            Well indeed it doesn't solve my problem, but I'm learning so I'm still happy to understand something better so thank anyway

            – Jaune
            Dec 12 '18 at 10:56











            2














            Hello Jaune and welcome to Stack Overflow. I have added an answer that will create the data first and then map that that data to elements using functions. If you have any questions please let me know.






            const field = ([x, y, color]) => {
            const td = document.createElement('td');
            td.setAttribute('data-x', x);
            td.setAttribute('data-y', y);
            td.className = color;
            td.innerHtml = `${x}-${y}`;
            return td;
            };
            const row = (row) => {
            const tr = document.createElement('tr');
            row.forEach((fieldItem) =>
            tr.appendChild(field(fieldItem)),
            );
            return tr;
            };
            const table = (tableData) => {
            const table = document.createElement('table');
            tableData.forEach((rowData) =>
            table.appendChild(row(rowData)),
            );
            return table;
            };
            const tableData = Array.from(Array(8).keys()).map((x) =>
            Array.from(Array(8).keys()).map((y) => [
            x,
            y,
            (x + y) % 2 ? 'black' : 'white',
            ]),
            );
            document.body.appendChild(table(tableData));

            .black {
            width:10px;
            height:10px;
            background-color: black;
            }
            .white {
            width:10px;
            height:10px;
            background-color: white;
            }
            table {
            border: 1px solid black;
            }








            share|improve this answer
























            • Hello HMR, and thanks for the warm welcome. I'd gladly take your proposal for help, but I'm still a little too fresh with the syntax to have interesting question about your code at the moment. Thanks again tho

              – Jaune
              Dec 12 '18 at 11:28






            • 1





              @Jaune The following book can help you out with syntax and here are some handy array functions.

              – HMR
              Dec 12 '18 at 11:53






            • 1





              Great docs, thanks a lot !

              – Jaune
              Dec 12 '18 at 12:47











            • While this is certainly the preferable way to do it, I'd like to point out that the question was to produce a chessboard using Javascript alone.

              – Abion47
              Dec 12 '18 at 19:06
















            2














            Hello Jaune and welcome to Stack Overflow. I have added an answer that will create the data first and then map that that data to elements using functions. If you have any questions please let me know.






            const field = ([x, y, color]) => {
            const td = document.createElement('td');
            td.setAttribute('data-x', x);
            td.setAttribute('data-y', y);
            td.className = color;
            td.innerHtml = `${x}-${y}`;
            return td;
            };
            const row = (row) => {
            const tr = document.createElement('tr');
            row.forEach((fieldItem) =>
            tr.appendChild(field(fieldItem)),
            );
            return tr;
            };
            const table = (tableData) => {
            const table = document.createElement('table');
            tableData.forEach((rowData) =>
            table.appendChild(row(rowData)),
            );
            return table;
            };
            const tableData = Array.from(Array(8).keys()).map((x) =>
            Array.from(Array(8).keys()).map((y) => [
            x,
            y,
            (x + y) % 2 ? 'black' : 'white',
            ]),
            );
            document.body.appendChild(table(tableData));

            .black {
            width:10px;
            height:10px;
            background-color: black;
            }
            .white {
            width:10px;
            height:10px;
            background-color: white;
            }
            table {
            border: 1px solid black;
            }








            share|improve this answer
























            • Hello HMR, and thanks for the warm welcome. I'd gladly take your proposal for help, but I'm still a little too fresh with the syntax to have interesting question about your code at the moment. Thanks again tho

              – Jaune
              Dec 12 '18 at 11:28






            • 1





              @Jaune The following book can help you out with syntax and here are some handy array functions.

              – HMR
              Dec 12 '18 at 11:53






            • 1





              Great docs, thanks a lot !

              – Jaune
              Dec 12 '18 at 12:47











            • While this is certainly the preferable way to do it, I'd like to point out that the question was to produce a chessboard using Javascript alone.

              – Abion47
              Dec 12 '18 at 19:06














            2












            2








            2







            Hello Jaune and welcome to Stack Overflow. I have added an answer that will create the data first and then map that that data to elements using functions. If you have any questions please let me know.






            const field = ([x, y, color]) => {
            const td = document.createElement('td');
            td.setAttribute('data-x', x);
            td.setAttribute('data-y', y);
            td.className = color;
            td.innerHtml = `${x}-${y}`;
            return td;
            };
            const row = (row) => {
            const tr = document.createElement('tr');
            row.forEach((fieldItem) =>
            tr.appendChild(field(fieldItem)),
            );
            return tr;
            };
            const table = (tableData) => {
            const table = document.createElement('table');
            tableData.forEach((rowData) =>
            table.appendChild(row(rowData)),
            );
            return table;
            };
            const tableData = Array.from(Array(8).keys()).map((x) =>
            Array.from(Array(8).keys()).map((y) => [
            x,
            y,
            (x + y) % 2 ? 'black' : 'white',
            ]),
            );
            document.body.appendChild(table(tableData));

            .black {
            width:10px;
            height:10px;
            background-color: black;
            }
            .white {
            width:10px;
            height:10px;
            background-color: white;
            }
            table {
            border: 1px solid black;
            }








            share|improve this answer













            Hello Jaune and welcome to Stack Overflow. I have added an answer that will create the data first and then map that that data to elements using functions. If you have any questions please let me know.






            const field = ([x, y, color]) => {
            const td = document.createElement('td');
            td.setAttribute('data-x', x);
            td.setAttribute('data-y', y);
            td.className = color;
            td.innerHtml = `${x}-${y}`;
            return td;
            };
            const row = (row) => {
            const tr = document.createElement('tr');
            row.forEach((fieldItem) =>
            tr.appendChild(field(fieldItem)),
            );
            return tr;
            };
            const table = (tableData) => {
            const table = document.createElement('table');
            tableData.forEach((rowData) =>
            table.appendChild(row(rowData)),
            );
            return table;
            };
            const tableData = Array.from(Array(8).keys()).map((x) =>
            Array.from(Array(8).keys()).map((y) => [
            x,
            y,
            (x + y) % 2 ? 'black' : 'white',
            ]),
            );
            document.body.appendChild(table(tableData));

            .black {
            width:10px;
            height:10px;
            background-color: black;
            }
            .white {
            width:10px;
            height:10px;
            background-color: white;
            }
            table {
            border: 1px solid black;
            }








            const field = ([x, y, color]) => {
            const td = document.createElement('td');
            td.setAttribute('data-x', x);
            td.setAttribute('data-y', y);
            td.className = color;
            td.innerHtml = `${x}-${y}`;
            return td;
            };
            const row = (row) => {
            const tr = document.createElement('tr');
            row.forEach((fieldItem) =>
            tr.appendChild(field(fieldItem)),
            );
            return tr;
            };
            const table = (tableData) => {
            const table = document.createElement('table');
            tableData.forEach((rowData) =>
            table.appendChild(row(rowData)),
            );
            return table;
            };
            const tableData = Array.from(Array(8).keys()).map((x) =>
            Array.from(Array(8).keys()).map((y) => [
            x,
            y,
            (x + y) % 2 ? 'black' : 'white',
            ]),
            );
            document.body.appendChild(table(tableData));

            .black {
            width:10px;
            height:10px;
            background-color: black;
            }
            .white {
            width:10px;
            height:10px;
            background-color: white;
            }
            table {
            border: 1px solid black;
            }





            const field = ([x, y, color]) => {
            const td = document.createElement('td');
            td.setAttribute('data-x', x);
            td.setAttribute('data-y', y);
            td.className = color;
            td.innerHtml = `${x}-${y}`;
            return td;
            };
            const row = (row) => {
            const tr = document.createElement('tr');
            row.forEach((fieldItem) =>
            tr.appendChild(field(fieldItem)),
            );
            return tr;
            };
            const table = (tableData) => {
            const table = document.createElement('table');
            tableData.forEach((rowData) =>
            table.appendChild(row(rowData)),
            );
            return table;
            };
            const tableData = Array.from(Array(8).keys()).map((x) =>
            Array.from(Array(8).keys()).map((y) => [
            x,
            y,
            (x + y) % 2 ? 'black' : 'white',
            ]),
            );
            document.body.appendChild(table(tableData));

            .black {
            width:10px;
            height:10px;
            background-color: black;
            }
            .white {
            width:10px;
            height:10px;
            background-color: white;
            }
            table {
            border: 1px solid black;
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 12 '18 at 11:15









            HMRHMR

            13.9k113899




            13.9k113899













            • Hello HMR, and thanks for the warm welcome. I'd gladly take your proposal for help, but I'm still a little too fresh with the syntax to have interesting question about your code at the moment. Thanks again tho

              – Jaune
              Dec 12 '18 at 11:28






            • 1





              @Jaune The following book can help you out with syntax and here are some handy array functions.

              – HMR
              Dec 12 '18 at 11:53






            • 1





              Great docs, thanks a lot !

              – Jaune
              Dec 12 '18 at 12:47











            • While this is certainly the preferable way to do it, I'd like to point out that the question was to produce a chessboard using Javascript alone.

              – Abion47
              Dec 12 '18 at 19:06



















            • Hello HMR, and thanks for the warm welcome. I'd gladly take your proposal for help, but I'm still a little too fresh with the syntax to have interesting question about your code at the moment. Thanks again tho

              – Jaune
              Dec 12 '18 at 11:28






            • 1





              @Jaune The following book can help you out with syntax and here are some handy array functions.

              – HMR
              Dec 12 '18 at 11:53






            • 1





              Great docs, thanks a lot !

              – Jaune
              Dec 12 '18 at 12:47











            • While this is certainly the preferable way to do it, I'd like to point out that the question was to produce a chessboard using Javascript alone.

              – Abion47
              Dec 12 '18 at 19:06

















            Hello HMR, and thanks for the warm welcome. I'd gladly take your proposal for help, but I'm still a little too fresh with the syntax to have interesting question about your code at the moment. Thanks again tho

            – Jaune
            Dec 12 '18 at 11:28





            Hello HMR, and thanks for the warm welcome. I'd gladly take your proposal for help, but I'm still a little too fresh with the syntax to have interesting question about your code at the moment. Thanks again tho

            – Jaune
            Dec 12 '18 at 11:28




            1




            1





            @Jaune The following book can help you out with syntax and here are some handy array functions.

            – HMR
            Dec 12 '18 at 11:53





            @Jaune The following book can help you out with syntax and here are some handy array functions.

            – HMR
            Dec 12 '18 at 11:53




            1




            1





            Great docs, thanks a lot !

            – Jaune
            Dec 12 '18 at 12:47





            Great docs, thanks a lot !

            – Jaune
            Dec 12 '18 at 12:47













            While this is certainly the preferable way to do it, I'd like to point out that the question was to produce a chessboard using Javascript alone.

            – Abion47
            Dec 12 '18 at 19:06





            While this is certainly the preferable way to do it, I'd like to point out that the question was to produce a chessboard using Javascript alone.

            – Abion47
            Dec 12 '18 at 19:06











            1














            As an alternative approach, consider coloring the table itself black and only coloring the corresponding tiles white:






            window.onload = addElement;

            function addElement() {
            var tbl = document.createElement('table');
            tbl.style.backgroundColor = 'black';

            for (var i = 1; i < 9; i++) {
            var tr = document.createElement('tr');

            for (var j = 1; j < 9; j++) {
            var td = document.createElement('td');
            td.style.width = '25px';
            td.style.height = '25px';

            if (i % 2 == j % 2) {
            td.style.backgroundColor = 'white';
            }

            tr.appendChild(td);
            }

            tbl.appendChild(tr);
            }

            document.body.appendChild(tbl);
            }





            And also, as Alex G said, it's much simpler to do the styling at the time the element is created instead of trying to iterate over them later. I understand the mindset of separating the logic and style portions of the code, but in this case it isn't really worth the added effort and complexity, and there are better ways to do it anyway.



            And besides, if you wanted to separate the style from the logic, that's what CSS files are for. :P






            share|improve this answer
























            • I know I know, but the exercise being JS only, I didn't even had a .css or a <style> soooo it kinda messed my head up tbh :p Another student indeed went with the white td on black table thanks to your advice and it went well !

              – Jaune
              Dec 13 '18 at 12:57


















            1














            As an alternative approach, consider coloring the table itself black and only coloring the corresponding tiles white:






            window.onload = addElement;

            function addElement() {
            var tbl = document.createElement('table');
            tbl.style.backgroundColor = 'black';

            for (var i = 1; i < 9; i++) {
            var tr = document.createElement('tr');

            for (var j = 1; j < 9; j++) {
            var td = document.createElement('td');
            td.style.width = '25px';
            td.style.height = '25px';

            if (i % 2 == j % 2) {
            td.style.backgroundColor = 'white';
            }

            tr.appendChild(td);
            }

            tbl.appendChild(tr);
            }

            document.body.appendChild(tbl);
            }





            And also, as Alex G said, it's much simpler to do the styling at the time the element is created instead of trying to iterate over them later. I understand the mindset of separating the logic and style portions of the code, but in this case it isn't really worth the added effort and complexity, and there are better ways to do it anyway.



            And besides, if you wanted to separate the style from the logic, that's what CSS files are for. :P






            share|improve this answer
























            • I know I know, but the exercise being JS only, I didn't even had a .css or a <style> soooo it kinda messed my head up tbh :p Another student indeed went with the white td on black table thanks to your advice and it went well !

              – Jaune
              Dec 13 '18 at 12:57
















            1












            1








            1







            As an alternative approach, consider coloring the table itself black and only coloring the corresponding tiles white:






            window.onload = addElement;

            function addElement() {
            var tbl = document.createElement('table');
            tbl.style.backgroundColor = 'black';

            for (var i = 1; i < 9; i++) {
            var tr = document.createElement('tr');

            for (var j = 1; j < 9; j++) {
            var td = document.createElement('td');
            td.style.width = '25px';
            td.style.height = '25px';

            if (i % 2 == j % 2) {
            td.style.backgroundColor = 'white';
            }

            tr.appendChild(td);
            }

            tbl.appendChild(tr);
            }

            document.body.appendChild(tbl);
            }





            And also, as Alex G said, it's much simpler to do the styling at the time the element is created instead of trying to iterate over them later. I understand the mindset of separating the logic and style portions of the code, but in this case it isn't really worth the added effort and complexity, and there are better ways to do it anyway.



            And besides, if you wanted to separate the style from the logic, that's what CSS files are for. :P






            share|improve this answer













            As an alternative approach, consider coloring the table itself black and only coloring the corresponding tiles white:






            window.onload = addElement;

            function addElement() {
            var tbl = document.createElement('table');
            tbl.style.backgroundColor = 'black';

            for (var i = 1; i < 9; i++) {
            var tr = document.createElement('tr');

            for (var j = 1; j < 9; j++) {
            var td = document.createElement('td');
            td.style.width = '25px';
            td.style.height = '25px';

            if (i % 2 == j % 2) {
            td.style.backgroundColor = 'white';
            }

            tr.appendChild(td);
            }

            tbl.appendChild(tr);
            }

            document.body.appendChild(tbl);
            }





            And also, as Alex G said, it's much simpler to do the styling at the time the element is created instead of trying to iterate over them later. I understand the mindset of separating the logic and style portions of the code, but in this case it isn't really worth the added effort and complexity, and there are better ways to do it anyway.



            And besides, if you wanted to separate the style from the logic, that's what CSS files are for. :P






            window.onload = addElement;

            function addElement() {
            var tbl = document.createElement('table');
            tbl.style.backgroundColor = 'black';

            for (var i = 1; i < 9; i++) {
            var tr = document.createElement('tr');

            for (var j = 1; j < 9; j++) {
            var td = document.createElement('td');
            td.style.width = '25px';
            td.style.height = '25px';

            if (i % 2 == j % 2) {
            td.style.backgroundColor = 'white';
            }

            tr.appendChild(td);
            }

            tbl.appendChild(tr);
            }

            document.body.appendChild(tbl);
            }





            window.onload = addElement;

            function addElement() {
            var tbl = document.createElement('table');
            tbl.style.backgroundColor = 'black';

            for (var i = 1; i < 9; i++) {
            var tr = document.createElement('tr');

            for (var j = 1; j < 9; j++) {
            var td = document.createElement('td');
            td.style.width = '25px';
            td.style.height = '25px';

            if (i % 2 == j % 2) {
            td.style.backgroundColor = 'white';
            }

            tr.appendChild(td);
            }

            tbl.appendChild(tr);
            }

            document.body.appendChild(tbl);
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 12 '18 at 19:03









            Abion47Abion47

            6,51821639




            6,51821639













            • I know I know, but the exercise being JS only, I didn't even had a .css or a <style> soooo it kinda messed my head up tbh :p Another student indeed went with the white td on black table thanks to your advice and it went well !

              – Jaune
              Dec 13 '18 at 12:57





















            • I know I know, but the exercise being JS only, I didn't even had a .css or a <style> soooo it kinda messed my head up tbh :p Another student indeed went with the white td on black table thanks to your advice and it went well !

              – Jaune
              Dec 13 '18 at 12:57



















            I know I know, but the exercise being JS only, I didn't even had a .css or a <style> soooo it kinda messed my head up tbh :p Another student indeed went with the white td on black table thanks to your advice and it went well !

            – Jaune
            Dec 13 '18 at 12:57







            I know I know, but the exercise being JS only, I didn't even had a .css or a <style> soooo it kinda messed my head up tbh :p Another student indeed went with the white td on black table thanks to your advice and it went well !

            – Jaune
            Dec 13 '18 at 12:57




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53741270%2fchessboard-in-pure-js%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Le Mesnil-Réaume

            Ida-Boy-Ed-Garten

            web3.py web3.isConnected() returns false always