Skip to content

Instantly share code, notes, and snippets.

@tomhodgins
Created May 16, 2020 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomhodgins/b30524b88a20ad0ac2b91ff19c9ab309 to your computer and use it in GitHub Desktop.
Save tomhodgins/b30524b88a20ad0ac2b91ff19c9ab309 to your computer and use it in GitHub Desktop.
Various ways JS can create an element in an HTML document
// Ways to create a <div> from JS
// write
document.write('<div></div>')
// createElement
document.createElement('<div></div>')
// innerHTML
document.documentElement.innerHTML += '<div></div>'
// createContextualFragment
document.createRange().createContextualFragment('<div></div>')
// DOMParser
new DOMParser().parseFromString('<div></div>', 'text/html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment