Created
April 12, 2020 02:37
-
-
Save cannandev/d888fc728bb4ba26cdfd5a7eeffca018 to your computer and use it in GitHub Desktop.
JS Chess Board
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table> | |
<tbody id="grid"></tbody> | |
</table> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var grid = document.getElementById('grid'); | |
var size = 8; | |
for (var i=1; i <= size; i++) { | |
var row = document.createElement('tr'); | |
for (var j=0; j < size; j++) { | |
var cell = document.createElement('td'); | |
if ((i+j) % 2 !== 0) { | |
row.appendChild(cell); | |
cell.classList.add('odd'); | |
} | |
else { | |
row.appendChild(cell); | |
cell.classList.add('even'); | |
} | |
grid.appendChild(row); | |
row.id = 'row-' + i; | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
table { | |
border: 2px solid black; | |
margin: 0 auto; | |
} | |
td { | |
height: 50px; | |
width: 50px; | |
text-align: center; | |
color: salmon; | |
} | |
.even { | |
background-color: black; | |
} | |
.odd { | |
background-color: ivory; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment