How can I extract text from a specific HTML tag using JavaScript? [duplicate]
![How can I extract text from a specific HTML tag using JavaScript? [duplicate]](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2F80wy3gkl%2Fproduction%2Fe6181cc418095a9f2e6e055e375b58562cbfb341-1280x720.png%3Fh%3D1000&w=3840&q=75)
I'm working on a simple web project where I need to dynamically extract the text from a specific HTML element using JavaScript. For example, I have a with an id, and I want to get the text content from inside that element and use it elsewhere in my script.
I’ve tried using document.getElementById("quote") but I’m not sure if I should use .value, .innerHTML, or .textContent to get just the plain text. I also want to make sure this works reliably in most browsers and doesn’t return any extra HTML or quotes — just the clean string.
Also, if there are better ways to access or manipulate text content from the DOM, I’d appreciate a quick explanation or best practices.
This gave me the text, but I read that innerHTML might include HTML tags if there are any, which I don’t want.
Answer
To extract only the plain text from an HTML element using JavaScript, the best and most reliable approach is to use .textContent
.
Here’s how you can do it:
Enjoyed this question?
Check out more content on our blog or follow us on social media.
Browse more questions