Categories
JavaScript Answers

How to convert a HTMLElement to a string with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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