For reusable code, I would like to make a JS function that takes an array of strings to turn into a top centered horizontal navbar and the id of where to put it. For this example we will make a few assumptions; namely that each string in the array represents the html filename, and the name we want for the button. Further, we will be using the w3.css 2.82.
So first the JS function:
function makeNavigation(navArray, navID) {
var numNav = navArray.length;
var spaceAllocated = (100 - 2) / numNav;
var theID = document.getElementById(navID);
for (var i = 0; i < numNav; i++) {
theID.appendChild('<li style = "width:"'+spaceAllocated+'%><a href="#'+navArray[i]+'.html">'+navArray[i]+'</a></li>');
}
in the HTML, we have
<head>
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
</head>
<div>
<ul id="test" class="w3-navbar w3-border w3-light-grey w3-center">
</ul>
</div>
<script>makeNavigation(["PageOne","PageTwo","PageThree"], "test")</script>
but this doesn't work. I have also just tried appendChild...