So there exist two other sets of insertion methods to handle all cases easily. We can easily see that the second method is much shorter.
So, these methods can only be used to insert DOM nodes or text pieces. It is inherently more resource-intensive to use these methods if your script is intended to write the HTML from which the browser creates the DOM in the first place. Using insertAdjacentHTML is not a direct substitute as the browser closes tags which the script requires remain open, and takes twice as long to ultimately create a mangled page.
Writing all the pieces to a string and then passing it to insertAdjacentHTML takes even longer, but at least you get the page as designed. But usually the returned value is not used, we just run the method. All insertion methods automatically remove the node from the old place.
Methods to create new nodes: The first one exists for historical reasons. If we call it afterwards, the existing document content is erased. The call to document. We could make a function and put the code there.
I just tested using an onload param in the body tag and even at this point the document is still open and document. To create DOM nodes, there are two methods: We can easily notice similarities between this and the previous picture.
Of course, all that is doable, but not in an elegant way.
This set of methods provides more flexible insertions: That was the downside. In modern scripts we can rarely see it, because of the following important limitation: For instance, here a string and an element are inserted: By the time it is finished this script creates over DOM nodes, mostly table cells.
But in practice these requirements rarely come together. Sometimes when we have a big element, that may be faster and simpler.Note: as mint-body.com writes to the document stream, calling mint-body.com on a closed (loaded) document automatically calls mint-body.com, which will clear the document.
Also, the input values of your form won't get magically transported into that array, I hope you're aware of that. It's beyond the scope of this question to explain how to read form input values, but I can tell you how to trigger the script so you actually see something when not in a JSBin: wrap all your script in a function such as function.
Note: When this method is not used for testing, it is often used to write some text to.Download