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.