Given an HTML document containing some elements and the task is to get the text inside an HTML element using JavaScript. There are two methods to get the text without HTML element which are listed below:
- Using innerText property
- Using textContent property
Using innerText property: We can use innerText property to get the text from HTML element.
Example:
<!DOCTYPE html> <html> <head> <title> Get the text inside HTML element using JavaScript </title> </head> <body> <div class="main"> Welcome to GeeksforGeeks </div> <script> const div = document.querySelector('.main'); alert(div.innerText); </script> </body> </html> |
Output:

Using textContent property: We can also use textContent property to get the text from HTML element.
Example:
<!DOCTYPE html> <html> <head> <title> Get the text inside HTML element using JavaScript </title> </head> <body> <div class="main"> Welcome to GeeksforGeeks </div> <script> const div = document.querySelector('.main'); alert(div.textContent); </script> </body> </html> |
Output:

Difference between innerText and textContent:
- The
innerTextproperty returns only human-readable text while thetextContentproperty returns all the text including<script>and<style>tag. - The
innerTextproperty take care of styling of elements and did not return any hidden elements whiletextContentproperty returns all the elements including the hidden ones. - The
innerTextproperty is defined only for HTMLElement objects, whereastextContentproperty is defined for all Node objects.
Recommended Posts:
- How to create long shadow of text without using text-shadow in HTML and CSS ?
- How to select all text in HTML text input when clicked using JavaScript?
- JavaScript | Get the text of a span element
- Text Animation with changing the color of the text using HTML & CSS
- How to get the value of text input field using JavaScript?
- How to get the text of option tag by value using JavaScript ?
- JQuery | Get the text of a span element
- How to get the Highlighted/Selected text in JavaScript?
- Delete the first element of array without using shift() method in JavaScript
- How to get/change the HTML with DOM element in JavaScript ?
- JavaScript method to get the URL without query string
- How to get all property values of a JavaScript Object (without knowing the keys) ?
- How to add tooltip to HTML table cell without using JavaScript ?
- How to get the child element of a parent using JavaScript ?
- How to get the data attributes of an element using JavaScript ?
- How to get the type of DOM element using JavaScript?
- How to get the class of a element which has fired an event using JavaScript/JQuery?
- How to get a notification when an element is added to the page using JavaScript ?
- How to get all CSS styles that are applied directly to an element using JavaScript ?
- How to get text of specific option tag using jQuery?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


