I am trying to add a header to a html table with a javascript function. The entire script is rather large so I've just included the html snippet where the html table is created and then the function that is supposed to add a header to that table. (when a button is clicked this function will be called). However I get the following error:
Locations.html:703 Uncaught TypeError: Cannot read property 'createTHead' of null
Line 703 corresponds with the "var hdr = tbs.createTHead();" line of code. It seems as though the function cannot find the "myTable" table and is returning a null when it runs the getelement by id line (i think)..but I am not sure why? Why would it not be able find this table element?
<div class="statsContainer">
<div class="mainCont" id="mainCont"></div>
<div class="statsLoc" id="statsLoc">
<table class="myTable" id="myTable"></table>
</div>
<div class="listLoc" id="listLoc"></div>
</div>
function locClick(strLN) {
RegionName.innerText = strLN;
var qry = "select Latitude, Longitude, Description from locations where Location_ID in (select Location_ID from locations where Location_Name = '" + strLN + "')";
clearDiv("mainCont");
clearDiv("statsLoc");
clearDiv("listLoc");
phpRequest(qry, "getDataFromPhp", processPHP, 1);
addTableHead("myTable", "help");
}
function addTableHead(tbName, txtHead) {
var tbs = document.getElementById("myTable");
var hdr = tbs.createTHead();
var rw = hdr.insertRow(0);
var cell = rw.insertCell(0);
cell.innerHTML = txtHead;}