Sometimes, we want to convert a HTMLElement to a string with JavaScript.
In this article, we’ll look at how to convert a HTMLElement to a string with JavaScript.
How to convert a HTMLElement to a string with JavaScript?
To convert a HTMLElement to a string with JavaScript, we can use the outerHTML
property.
For instance, we write
const element = document.getElementById("new-element-1");
const elementHtml = element.outerHTML;
to get the element with getElementById
.
Then we get the HTML code of the element with the outerHTML
property.
Conclusion
To convert a HTMLElement to a string with JavaScript, we can use the outerHTML
property.