I've made a Knight's Tour game in javascript. You can try it here. (Particularly for reproducing the error)
The error I'm having so much trouble finding reads
Uncaught TypeError: Cannot read property 'innerHTML' of undefined
but I'm not sure why it's undefined. I have a table with 64 cells (making a chessboard). They're all built as plain and empty
<table border=1 style="border: 1px solid black; table-layout: fixed;">
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr><tr>
...
This is the only table on the page. I use javascript to retrieve all of the td elements and I iterate through them adding ids, classes, background color and onclick eventListeners for gameplay.
var tds;
window.onload = function() {
tds = document.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
if (typeof window.addEventListener === 'function') {
(function (td) {
...
td.addEventListener('click', function() {
...
There seems to be a few different cells that repeatably have this error occur. Sometimes I can get the individual cell to produce an error, other times I can't. One repeatable example is to move first on the bottom row, second to last cell, and then try to move onto the cell two up and one right from it along the side.
I read the innerHTML attribute frequently inside the onclick. I refer to the global tds array of td elements. Here are basically all forms of myself accessing them.
if (tds[cell].innerHTML === "") {
if (tds[n].innerHTML === "♞") {
tds[cell].innerHTML = "♞";
tds[target].innerHTML = "♘";
td.innerHTML = "♞";
I really thought I had it figured out when I was reading about the difference between declaring a variable using var and when not using it, but I couldn't figure out a single var that was causing this error. Any help would be appreciated.