Home > Back-end >  Can I get the raw HTML output from a JS HTML object?
Can I get the raw HTML output from a JS HTML object?

Time:06-25

I've created multiple HTML objects using

document.createElement("");

after that, I applied some modifiers like CSS, class names and onclick behaviors. How can I get the raw HTML code that is generated? My solution is creating an invisible div, adding the element as a child and call

document.getElementBy("my-invisible-div").innerHTML

to get the HTML. Is there a better way of doing this? I need it for a database export and I don't want to have my HTML code in a string with placeholders.

Thanks for any advice

CodePudding user response:

Does something like this work for you:

var element = document.createElement("div")
console.log(element.outerHTML)
  • Related