I have the following code:
tr = document.createElement("tr");
root.appendChild(tr);
td = document.createElement("td");
td.appendChild(document.createTextNode("some value"));
tr.appendChild(td);
Well, this code is same as
root.innerHTML = "<tr><td>some value</td></tr>";
The first version is probably a better way to go, because the browser does not have to render a string. But it is too long. Especially for bigger structures, this implementation is becoming really hard to read. So I felt like there is a better way to write this (without any JavaScript library). How would you implement this so the code would be more readable? (Right now I am separating code with comments.)
You could always write wrapper functions around the clunky DOM API to make the code more readable. I usually use something like:
Then your can write much more declarative code:
Just remember the differences between css and javascript: