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.