I am working on a webpage where scripts in both the head and section elements are used to output a table where the cells in the table change to a random color when clicked. The problem is the header and nav elements aren't showing when the page is loaded, but the table does and the scripts are working fine.
Being that I'm using a nested loop in the body section, I checked around this site for solutions but didn't see anything that could help me. I also tried to move around the table elements concatenated to a string variable, but that didn't work either. Finally(if this helps), I floated the nav element to the left in my external style sheet(not shown), and floated the section element to the left as well.
In the head element(including embedded css):
<script>
function myFunction(e) {
var colorstring = ["green", "lightgreen", "blue", "pink", "yellow", "purple", "brown", "red", "orange", "black"];
var index = Math.floor((Math.random() * 9) + 1);
e.innerHTML = colorstring[index];
e.style.backgroundColor = colorstring[index];
}
</script>
<style>
h3 {
text-align: center;
}
table {
margin: 0 auto;
width: 50%;
}
table,td {
border: 1px solid;
border-collapse: collapse;
}
table td {
text-align: center;
width: 25%;
height: 1.5em;
}
</style>
In the header, nav, and section element(all from the body element):
<header>
<img src="Fonts/Proj6_heading.png" />
</header>
<nav class="verticalNAV">
<ul>
<li><a href="Home_Page.html">Our Services</a></li>
<li><a href="Growing_Pumpkins.html">Growing Pumpkins</a></li>
<li><a href="Currency_Converter.html">Currency Converter</a></li>
<li><a href="Add_Table_Block.html">Add Table Block</a></li>
</ul>
</nav>
<section>
<h2><Add Table Block></h2>
<script>
var string = "";
string = string + "<h3>Add <Table> Block</h3>";
string = string + "<table>";
for (index = 0; index <= 4; index++) {
string = string + "<tr>";
for (index2 = 0; index2 <= 3; index2++) {
string = string + "<td onclick='myFunction(this)' width='20%'>Click Me!</td>"
}
string = string + "</tr>";
}
string = string + "</table>";
document.body.innerHTML = string;
</script>
</section>
function myFunction(e) {
var colorstring = ["green", "lightgreen", "blue", "pink", "yellow", "purple", "brown", "red", "orange", "black"];
var index = Math.floor((Math.random() * 9) + 1);
e.innerHTML = colorstring[index];
e.style.backgroundColor = colorstring[index];
}
var string = "";
string = string + "<h3>Add <Table> Block</h3>";
string = string + "<table>";
for (index = 0; index <= 4; index++) {
string = string + "<tr>";
for (index2 = 0; index2 <= 3; index2++) {
string = string + "<td onclick='myFunction(this)' width='20%'>Click Me!</td>"
}
string = string + "</tr>";
}
string = string + "</table>";
document.body.innerHTML = string;
h3 {
text-align: center;
}
table {
margin: 0 auto;
width: 50%;
}
table,
td {
border: 1px solid;
border-collapse: collapse;
}
table td {
text-align: center;
width: 25%;
height: 1.5em;
}
<header>
<img src="Fonts/Proj6_heading.png" />
</header>
<nav class="verticalNAV">
<ul>
<li><a href="Home_Page.html">Our Services</a></li>
<li><a href="Growing_Pumpkins.html">Growing Pumpkins</a></li>
<li><a href="Currency_Converter.html">Currency Converter</a></li>
<li><a href="Add_Table_Block.html">Add Table Block</a></li>
</ul>
</nav>
<section>
<h2><Add Table Block></h2>
</section>
document.body.innerHTML = string;
you are replacing all the html....