Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieving Incorrect Table Coordinates
    text
    copied!<p>I created a checkers board using tables. I want to retrieve the x and y coordinates of a cell. However, the <code>this.parentNode.rowIndex</code> keeps giving me -1. I've spent countless hours looking for the error. Can anyone find it for me?</p> <pre><code>var board = [[1, 0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, -1, 0, -1, 0, -1, 0, -1], [-1, 0, -1, 0, -1, 0, -1, 0], [0, -1, 0, -1, 0, -1, 0, -1]]; var gray = -1; //distinguishing players var red = 1; function makeBoard() { //create a table var tbl = document.createElement("table"); //create a &lt;tr&gt; for each row for (var i = 0; i &lt; 8; i++) { var tr = document.createElement("tr"); //create a &lt;td&gt; for each column for (var j = 0; j &lt; 8; j++) { var td = document.createElement("td"); //setting the attributes of a square td.setAttribute("width", "50"); td.setAttribute("height", "50"); if ((i % 2 == 0 &amp;&amp; j % 2 != 0) || (i % 2 !=0 &amp;&amp; j % 2 == 0)) { td.style.backgroundColor = "black"; } else if (board[i][j] == red) { td.style.backgroundColor = "red"; } else if (board[i][j] == gray) { td.style.backgroundColor = "gray"; } td.onclick = function() { alert(this.cellIndex + ", " + this.parentNode.rowIndex); //RETRIEVING WRONG COORDINATES } tr.appendChild(td); } tbl.appendChild(tr); } tbl.setAttribute("border", "10"); return tbl; } </code></pre> <p>If you feel that something is missing, please let me know.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload