I have done a fair bit of googling and looking at "you might not need jquery" but still haven't been able to figure out what is wrong.
I have a makeTable function that builds up a html string of elements to form a table. The function works great when I append the output table to the DOM using the Jquery .html method.
Here is the function:
function drawTable () {
var outputString = '';
for(var i = 1; i <= 12; i++ ){
outputString += "<tr>";
for(var j = 1; j <= 12; j++) {
outputString += "<td>" + i * j + "</>";
}
outputString += "</tr>";
}
$("#output").html(outputString);
}
drawTable();
However, when I don't use Jquery and I change $("#output").html(outputString); to document.getElementById('output').innerHTML(outputString); the table doesn't render on the page. I just get a single line of numbers. When I inspect the element, <tr> and <td> elements are not being rendered at all and I don't understand why.
I am confused. How should I render this table in plain JS? What am I not understanding about the innerHTML method ? The functions are identical, I am just changing the line where outputString is appended to the DOM and really don't understand why it is not rendering correctly when I attempt to do it in plain JS. Please enlighten me!
"</>"is not an end tag.innerHTMLis not a getter/setter method like jQuery's.html(), it's a property of a DOM element. You need to assignoutputString, e.g.element.innerHTML = outputString.id="output"is a<table>. See Plunker.