Javascript: AppendChild
Try wrapping your JavaScript in an onload function. So first add:
<body onload="load()">
Then put your javascript in the load function, so:
function load() {
var blah="Blah!";
var t = document.createElement("table"),
tb = document.createElement("tbody"),
tr = document.createElement("tr"),
td = document.createElement("td");
t.style.width = "100%";
t.style.borderCollapse = 'collapse';
td.appendChild(document.createTextNode(blah));
// note the reverse order of adding child
tr.appendChild(td);
tb.appendChild(tr);
t.appendChild(tb);
document.getElementById("theBlah").appendChild(t);
}
The script is being run before the page completes loading. Which is why document.getElementById("theBlah") returns null.
Either use something like jQuery or simply something like
<script>
window.onload = function () {
var blah="Blah!";
var t = document.createElement("table"),
tb = document.createElement("tbody"),
...
//the rest of your code here
};
</script>
The problem is that document.getElementById("theBlah")
returns null. The reason why is that your code is running before the theBlah
element has been created. You should place your code in an onload
event handler.