JavaScript – Get HTML Text inside an Element

To get the HTML text inside a specific HTML Element, using JavaScript, get reference to this HTML element, and read the innerHTML property of this HTML Element.

innerHTML property returns the HTML markup present inside this HTML Element as String.

In the following example, we will get the HTML text inside an Element, which is selected by id "myElement" and print it to console.

example.html

ADVERTISEMENT

Try this html file online, and click on the Click Me button. The script gets the HTML text inside the Element #myElement, and prints it to console as shown in the following screenshot.

Conclusion

In this JavaScript Tutorial, we learned how to get the HTML text inside an Element, using JavaScript.