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.