How do you look at the source? JavaScript generated elements do not appear in the 'View Source. Are you talking about innerHTML ?
If so, what you get is the serialization of the web browser of the DOM nodes in the document. In a browser, HTML page layout is not the ultimate repository for document state. The document is stored as loading Node objects; when these objects are serialized back to the markup, that markup may not look like the markup of the original HTML page that was parsed to get the document.
Therefore, regardless of which:
<img src="x" alt="y"/> <img src="x" alt="y"> <img alt = "y" src="x"> img= document.createElement('img'); img.src= 'x'; img.alt= 'y';
you use to create an HTMLImageElement node, when you serialize it using innerHTML , the browser usually generates the same HTML markup.
If the browser is in native-XML mode (i.e. since the page was presented as application/xhtml+html ), then the value of innerHTML will necessarily contain self-closing syntax, for example, <img/> . (In some browsers, you can also see other XML elements, such as namespaces.)
However, if, most likely, today you are using HTML-compatible XHTML under the text/html media type, the browser does not actually use XHTML at all, so you will get the old HTML school when accessing innerHTML and you will not see the self-closing form /> .
source share