Chessboard in pure js











up vote
8
down vote

favorite












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









New contributor




Jaune is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 9




    Quick tip - press F12 and you'll be able to see any errors in your console.
    – markmoxx
    5 hours ago















up vote
8
down vote

favorite












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









New contributor




Jaune is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 9




    Quick tip - press F12 and you'll be able to see any errors in your console.
    – markmoxx
    5 hours ago













up vote
8
down vote

favorite









up vote
8
down vote

favorite











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









New contributor




Jaune is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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









New contributor




Jaune is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Jaune is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 5 hours ago









markmoxx

579213




579213






New contributor




Jaune is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 5 hours ago









Jaune

463




463




New contributor




Jaune is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Jaune is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Jaune is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 9




    Quick tip - press F12 and you'll be able to see any errors in your console.
    – markmoxx
    5 hours ago














  • 9




    Quick tip - press F12 and you'll be able to see any errors in your console.
    – markmoxx
    5 hours ago








9




9




Quick tip - press F12 and you'll be able to see any errors in your console.
– markmoxx
5 hours ago




Quick tip - press F12 and you'll be able to see any errors in your console.
– markmoxx
5 hours ago












4 Answers
4






active

oldest

votes

















up vote
7
down vote



accepted










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





















  • Perfect ! Thanks a lot, it solves everything.
    – Jaune
    5 hours ago


















up vote
3
down vote













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
    5 hours ago










  • Oops, thanks @RolandStarke
    – markmoxx
    5 hours ago










  • 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
    5 hours ago


















up vote
3
down vote













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
    5 hours ago


















up vote
1
down vote













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
    4 hours ago






  • 1




    @Jaune The following book can help you out with syntax and here are some handy array functions.
    – HMR
    4 hours ago






  • 1




    Great docs, thanks a lot !
    – Jaune
    3 hours ago











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',
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
});


}
});






Jaune is a new contributor. Be nice, and check out our Code of Conduct.










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

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
7
down vote



accepted










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





















  • Perfect ! Thanks a lot, it solves everything.
    – Jaune
    5 hours ago















up vote
7
down vote



accepted










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





















  • Perfect ! Thanks a lot, it solves everything.
    – Jaune
    5 hours ago













up vote
7
down vote



accepted







up vote
7
down vote



accepted






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 5 hours ago









Karan

2,7252322




2,7252322












  • Perfect ! Thanks a lot, it solves everything.
    – Jaune
    5 hours ago


















  • Perfect ! Thanks a lot, it solves everything.
    – Jaune
    5 hours ago
















Perfect ! Thanks a lot, it solves everything.
– Jaune
5 hours ago




Perfect ! Thanks a lot, it solves everything.
– Jaune
5 hours ago












up vote
3
down vote













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
    5 hours ago










  • Oops, thanks @RolandStarke
    – markmoxx
    5 hours ago










  • 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
    5 hours ago















up vote
3
down vote













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
    5 hours ago










  • Oops, thanks @RolandStarke
    – markmoxx
    5 hours ago










  • 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
    5 hours ago













up vote
3
down vote










up vote
3
down vote









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 5 hours ago

























answered 5 hours ago









markmoxx

579213




579213








  • 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
    5 hours ago










  • Oops, thanks @RolandStarke
    – markmoxx
    5 hours ago










  • 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
    5 hours ago














  • 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
    5 hours ago










  • Oops, thanks @RolandStarke
    – markmoxx
    5 hours ago










  • 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
    5 hours ago








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
5 hours ago




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
5 hours ago












Oops, thanks @RolandStarke
– markmoxx
5 hours ago




Oops, thanks @RolandStarke
– markmoxx
5 hours ago












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
5 hours ago




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
5 hours ago










up vote
3
down vote













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
    5 hours ago















up vote
3
down vote













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
    5 hours ago













up vote
3
down vote










up vote
3
down vote









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 5 hours ago









Alex G

1,01529




1,01529












  • 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
    5 hours ago


















  • 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
    5 hours ago
















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
5 hours ago




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
5 hours ago










up vote
1
down vote













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
    4 hours ago






  • 1




    @Jaune The following book can help you out with syntax and here are some handy array functions.
    – HMR
    4 hours ago






  • 1




    Great docs, thanks a lot !
    – Jaune
    3 hours ago















up vote
1
down vote













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
    4 hours ago






  • 1




    @Jaune The following book can help you out with syntax and here are some handy array functions.
    – HMR
    4 hours ago






  • 1




    Great docs, thanks a lot !
    – Jaune
    3 hours ago













up vote
1
down vote










up vote
1
down vote









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 5 hours ago









HMR

13.3k113898




13.3k113898












  • 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
    4 hours ago






  • 1




    @Jaune The following book can help you out with syntax and here are some handy array functions.
    – HMR
    4 hours ago






  • 1




    Great docs, thanks a lot !
    – Jaune
    3 hours ago


















  • 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
    4 hours ago






  • 1




    @Jaune The following book can help you out with syntax and here are some handy array functions.
    – HMR
    4 hours ago






  • 1




    Great docs, thanks a lot !
    – Jaune
    3 hours ago
















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
4 hours ago




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
4 hours ago




1




1




@Jaune The following book can help you out with syntax and here are some handy array functions.
– HMR
4 hours ago




@Jaune The following book can help you out with syntax and here are some handy array functions.
– HMR
4 hours ago




1




1




Great docs, thanks a lot !
– Jaune
3 hours ago




Great docs, thanks a lot !
– Jaune
3 hours ago










Jaune is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Jaune is a new contributor. Be nice, and check out our Code of Conduct.













Jaune is a new contributor. Be nice, and check out our Code of Conduct.












Jaune is a new contributor. Be nice, and check out our Code of Conduct.
















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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

Trompette piccolo

Slow SSRS Report in dynamic grouping and multiple parameters

Simon Yates (cyclisme)