I've got an HTML
string
e.g. '<p><span class="text">Hello World!</span></p>'
I parse this HTML
string
to DOM
using DOMParser().parseFromString()
, because I want to change the innerHTML
of some specific elements
. That's why I parse
the HTML
string
to DOM
and with getElementByClassName
I get all my elements and loop through them to change it innerHTML
. This works so far.
After I changed the innerHTML
, I try to convert the new DOM
back to a string
. I can't figure out how. What I tried is to assign the innerHTML
or outerHTML
(tried both) to a variable like this:
const parser = new DOMParser();
const doc = parser.parseFromString("<p>Hello World!</p>", "text/html");
console.log(doc.innerHTML) // undefined
console.log(doc.outerHTML) // undefined
const parser = new DOMParser();
const doc = parser.parseFromString("<p>Hello World!</p>", "text/html");
console.log(doc.innerHTML) // undefined
console.log(doc.outerHTML) // undefined
I always get undefined
. How can I parse it back to a string
? I found a lot examples with innerHTML
or outerHTML
, but in my case something went wrong. Any ideas?