dots and boxes example
Question from: http://stackoverflow.com/questions/31151150/javascript-checking-surrounding-table-cells
by pisamce
HTML
<h3 id="turn"></h3>
<table>
<tr>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
</tr>
<tr>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box" onclick="fillBox(this)"></td>
<td class="vLine" onclick="addLine(this)"></td>
</tr>
<tr>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
<td class="hLine" onclick="addLine(this)"></td>
<td class="gap"></td>
</tr>
<tr>
<td class="vLine" onclick="addLine(this)"></td>
<td class="box"...
CSS
h3 {
font-family: Arial;
}
table {
border-collapse: collapse;
}
.vLine {
width: 10px;
height: 60px;
}
.box {
width: 60px;
height: 60px;
}
.hLine {
width: 60px;
height: 10px;
}
.gap {
width: 10px;
height: 12px;
background-color: black;
}
.vLine:hover, .hLine:hover {
background-color: black;
}
JavaScript
var bgcolor = 'black';
var hlines = document.getElementsByClassName('hLine');
for(var i=0;i<7;i++){
for(var j=0;j<6;j++){
hlines[j+i*6].setAttribute('data-row', i);
hlines[j+i*6].setAttribute('data-col', j);
hlines[j+i*6].setAttribute('data-type', 'h');
}
}
var vlines = document.getElementsByClassName('vLine');
for(var i=0;i<6;i++){
for(var j=0;j<7;j++){
vlines[j+i*7].setAttribute('data-row', i);
vlines[j+i*7].setAttribute('data-col', j);
vlines[j+i*7].setAttribute('data-type', 'v');
}
}
var boxes = document.getElementsByClassName('box');
for(var i=0;i<6;i++){
for(var j=0;j<6;j++){
boxes[j+i*6].setAttribute('data-row', i);
boxes[j+i*6].setAttribute('data-col', j);
boxes[j+i*6].setAttribute('data-type', 'b');
}
}
function checkHorizontal(row, col){
//check up
if(row > 0
&& hlines[(row-1)*6+col].style.backgroundColor === bgcolor
&& hlines[row*6+col].style.backgroundColor === bgcolor
&& vlines[(row-1)*7+col].style.backgroundColor === bgcolor
&& vlines[(row-1)*7+col+1].style.backgroundColor === bgcolor){
fillBox(boxes[(row-1)*6+col]);
}
//check down
if(row < 6
&& hlines[(row+1)*6+col].style.backgroundColor === bgcolor
&& hlines[row*6+col].style.backgroundColor === bgcolor
&& vlines[row*7+col].style.backgroundColor === bgcolor
&& vlines[row*7+col+1].style.backgroundColor === bgcolor){
fillBox(boxes[row*6+col]);
}
}
function checkVertical(row, col){
//check left
if(col > 0
&& hlines[row*6+col-1].style.backgroundColor === bgcolor
&& hlines[(row+1)*6+col-1].style.backgroundColor === bgcolor
&& vlines[row*7+col].style.backgroundColor === bgcolor
&& vlines[row*7+col-1].style.backgroundColor === bgcolor){
fillBox(boxes[row*6+col-1]);
}
//check right
if(col < 5
&& hlines[row*6+col].style.backgroundColor === bgcolor
...