Categories
JavaScript Answers

How to Get Paragraph Text Inside an Element with JavaScript?

Spread the love

Sometimes, we want to get paragraph text inside an element with JavaScript.

In this article, we’ll look at how to get paragraph text inside an element with JavaScript.

Get Paragraph Text Inside an Element with JavaScript

To get paragraph text inside an element with JavaScript, we can use the innerHTML property of the element.

For instance, if we have:

<p>
  hello world
</p>

Then we write:

const text = document.querySelector('p').innerHTML;
console.log(text)

to get the p element with document.querySelector.

Then we use the innerHTML property to return the text.

Therefore, from the console log, we see that text is:

  hello world

Conclusion

To get paragraph text inside an element with JavaScript, we can use the innerHTML property of the element.

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 *