I am learning this stuff and trying to understand it, so am using this instead of innerhtml.
My code:
<script>
function load() {
"use strict";
var t = document.createElement("table"),
tb = document.createElement("tbody"),
tr = document.createElement("tr"),
td = document.createElement("td");
var b = document.createElement("b");
b.appendChild(document.createTextNode(" name!"));
td.appendChild(document.createTextNode("Hello"));
td.appendChild(b);
t.style.width = "100%";
t.style.borderCollapse = 'collapse';
t.border=1;
// note the reverse order of adding child
tr.appendChild(td);
tb.appendChild(tr);
t.appendChild(tb);
// more code to populate table rows and cells
document.getElementById("theBlah").appendChild(t);
alert(t.innerHTML);
}
</script>
</head>
<body onload="load()">
problem is , onload() does not display the <table> tag, it displays from <tbody> instead… any idea why?
And finally, in this code:
var t = document.createElement("table"),
tb = document.createElement("tbody"),
tr = document.createElement("tr"),
td = document.createElement("td");
even though I am using the Var keyword just for t, and not for the others, why is it not complaining even though “use strict” is on? In fact if I add a “var” in front of “tb, “tr”, “td” then it throws an error…
Thanks!
1) innerHTML is just that. It’s
inner. It doesn’t contain the HTML of the element itself, but all the html that is within that element. There is a property called outerHTML too, but it’s not included in Firefox. The only cross browser way to get outerHTML is to wrap the node in a new parent node and grab that nodes innerHTML. You can’t just grab the current parent’s innerHTML (You have to make a new parent) unless you’re sure the element has no siblings, because if you do the siblings will included.2) You are using commas to separate your variable decorations. That is the same as using semi-colons and putting
varon each line.