In: Computer Science
Javascript: How to change the background color of a TD on click and when the TD is clicked again the background color goes back to white. Here is the code I have so far.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
table {
border-collapse: collapse;
margin: auto;
}
th,
td {
border: 1px solid black;
vertical-align: middle;
}
td {
width: 20px;
height: 20px;
font-size: .85em;
text-align: center;
}
</style>
<script>
document.getElementsByTagName('td').addEventListener("click",
changeColor);
function(changeColor) {
document.getElementsByTagName("td").style.color = "#475892";
}
function(apocalypse) {
location.reload();
}
</script>
</head>
<body>
<table id="grid">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<button onclick="apocalypse()">Blinkers and
Crawlers</button>
<button onclick="apocalypse()">Evolve</button>
<button
onclick="apocalypse()">Evolve(Auto)</button>
<button
onclick="apocalypse()">Apocalypse</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
table {
border-collapse: collapse;
margin: auto;
}
th,
td {
border: 1px solid black;
vertical-align: middle;
}
td {
width: 20px;
height: 20px;
font-size: .85em;
text-align: center;
}
  
.highlight {
background-color: #475892;    
}
</style>
</head>
<body>
<table id="grid">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<button onclick="apocalypse()">Blinkers and Crawlers</button>
<button onclick="apocalypse()">Evolve</button>
<button onclick="apocalypse()">Evolve(Auto)</button>
<button onclick="apocalypse()">Apocalypse</button>
  
  
<script>
function colorChange(e) {
  e.target.classList.toggle('highlight');
};
var cells = document.getElementsByTagName("td"); 
for (var i = 0; i < cells.length; i++) { 
   cells[i].onclick = colorChange;
}
function apocalypse() {
  location.reload();
}
  
  
</script>
  
</body>
</html>

Please upvote, as i have given the exact answer as asked in
question. Still in case of any concerns in code, let me know in
comments. Thanks!