If I write this:
document.createElement("img")
The generated html is: <img>, and I can add attributes on this element. However, is there a parameter or something I could pass to createElement to make it self-closing? Or is there a dom function I could call to generate the </a> closing tag?
It doesn’t matter if tags are self-closing or not when they’ve been processed by the DOM. If you type
<img/>in the source and then look atinnerHTML, you’ll see<img>instead.Similarly, you don’t need to “generate” the closing tag because it’s already there. The browser has already taken your text-based HTML and turned it into a tree of nodes. Using DOM functions affects the tree, and getting
innerHTMLjust turns it into one of many possible text formats that produce the same tree.