Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I close an element created by document.createElement?

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?

like image 700
Geo Avatar asked Dec 28 '22 03:12

Geo


1 Answers

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 at innerHTML, 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 innerHTML just turns it into one of many possible text formats that produce the same tree.

like image 200
Niet the Dark Absol Avatar answered Dec 29 '22 17:12

Niet the Dark Absol