When generating HTML with JavaScript, is it better to use the annoyingly verbose
document.createElement("p")
...
(as seen here: https://www.w3schools.com/jsref/met_document_createelement.asp )
or is it better to use template literals (back-ticks)?
`<p></p>`
I'm not sure if template literals updates the DOM or not & I assume that the createElement() does?
And I don't know how to test it.
Example code:
<body>
<div id="myDIV"></div>
<script>
let theDiv = document.getElementById("myDIV");
function verboseGeneration(){
var para = document.createElement("P");
para.innerHTML = "This is a paragraph.";
theDiv.insertAdjacentElement("beforeend", para);
}
function TemplateLiteralGeneration(){
theDiv.insertAdjacentHTML("beforeend", `<p>This is a paragraph.</p>`)
}
verboseGeneration();
TemplateLiteralGeneration();
</script>
</body>
Expected result: They are equal.
Actual result: Unknown, since I don't know how to test how it interact with the HTML DOM.
(I'm planning on generating a big table based on some arrays, and I'm hesitant to use Template Literals in case they don't update or don't work properly with the DOM and it creates weird issues in the future.)