Categories
JavaScript Answers

How to get the text of a div tag using only JavaScript?

Spread the love

Sometimes, we want to get the text of a div tag using only JavaScript.

In this article, we’ll look at how to get the text of a div tag using only JavaScript.

How to get the text of a div tag using only JavaScript?

To get the text of a div tag using only JavaScript, we can use the textContent property.

For instance, we write

const el = document.getElementById("foo");
const textContent = el.textContent;

to select the element with getElementById.

Then we get the text of the element with the textContent property.

Conclusion

To get the text of a div tag using only JavaScript, we can use the textContent 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 *