Categories
JavaScript Answers

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 can use the outerHTML property.

For instance, we write

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

to select the element with getElementById.

Then we use the outerHTML property to get the HTML string for the element.

Conclusion

To get the HTML for a DOM element in 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 *