How to convert an HTMLElement to a string with JavaScript?

Spread the love

To convert an HTMLElement to a string with JavaScript, we use the outerHTML property.

For instance, we write

const element = document.getElementById("new-element-1");
const elementHtml = element.outerHTML;

to select the element with getElementById.

And then we get the HTML code for the element as a string with the outerHTML property.

Leave a Reply

Your email address will not be published. Required fields are marked *