Currently, I am tackling a webpage project that involves using scripts in both the head and section elements to generate a table where clicking on the cells changes their color randomly. However, a glitch seems to be hindering the display of the header and navigation elements upon page load, even though the table and scripts are functioning correctly.
My approach includes utilizing a nested loop in the body section, but I have scoured this platform for potential solutions without success. I also experimented with rearranging the table elements concatenated to a string variable, but that did not resolve the issue either. As a last resort, I adjusted the float property for the nav and section elements in my external stylesheet (not provided).
Within 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>
Include the header, nav, and section elements (all within 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>