How to get the HTML for a DOM element in JavaScript?

Spread the love

Sometimes, we want to get the HTML for a DOM element in JavaScript.

In this article, we’ll look at how to get the HTML for a DOM element in JavaScript.

How to get the HTML for a DOM element in JavaScript?

To get the HTML for a DOM element in JavaScript, we use the outerHTML property.

For instance, we write

const el = document.getElementById("foo");
console.log(el.outerHTML);

to get the element with getElementById.

Then we get its HTML content as a string with outerHTML.

Conclusion

To get the HTML for a DOM element in JavaScript, we use the outerHTML property.

Leave a Reply

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