JavaScript

innerText property in JavaScript | Explained

The innerText property of JavaScript is used to get or set the text inside an HTML element and its descendants. The innerText has a similar working phenomenon to innerHTML. Both properties manipulate the content of an HTML element but with different aspects. The innerText focuses on the textual content and the innerHTML considers the HTML content of an element.

This article provides an insight into the innerText property to achieve the following targets.

How the innerText property works in JavaScript

The working of innerText depends on the following syntaxes.

To get the text of an HTML element:

node.innerText;

To set/update the text of an HTML element:

node.innerText="text"

In the above syntaxes:

  • The node refers to the HTML element and all its descendants.
  • The text represents the new text that would be updated or set in place of the existing text.

How to use the innerText property in JavaScript

The following examples demonstrate the usage of the innerText property in HTML.

Example 1: Get the text of an HTML element

We have illustrated this example to show how text of an element can be obtained using innerText. property.

HTML

<p id="p1">

This is an <small>example</small> of<strong> innerText </strong>

</p>

<button onclick="get()"> Click here to get the innertext </button>

In the above code, a paragraph(id=”p1“) is created that contains a small tag and a strong tag. Moreover, a get() function is called on the click of the button.

JavaScript

function get(){

alert(document.getElementById("p1").innerText);

}

A function named get() is created that contains an alert statement to display the text of an element(id=”p1“).

Text Description automatically generated

Output

It is observed that the text of the paragraph(id=”p1“) and all its descendants(span and strong) is displayed.

Example 2: Update the text of an HTML element

The HTML and JS code provided below assist in updating the text of the element.

HTML

<p id="p1"> This is an example of innerText property </p>

<button onclick="update()"> Click here to update the innertext </button>

The HTML code creates a paragraph with id=”p1” and button that executes the update() function on its onclick property.

JavaScript

function update(){

document.getElementById("p1").innerText= "The text has been updated!";

}

The JavaScript code provided above creates an update() function that applies the innerText property to the paragraph with id=”p1“.

Text Description automatically generated

Output

Graphical user interface, text, application, email Description automatically generated

It is observed from the output that the text of the paragraph has been updated to new text.

Example 3: Set the text for an HTML element

In this example, the text of one element will be placed inside the other element and the following code assists in doing so.

HTML

<p id="old"> Welcome to Linuxhint </p>

<button onclick="set()"> Click here to set the innertext </button>

<h2 id="new"> </h2>

The HTML code contains a paragraph with id=”old“, a button to trigger the set() function, and a heading with id=”new“.

JavaScript

function set(){

document.getElementById("new").innerText=document.getElementById("old").innerText;

}

The above code gets the text of a paragraph element (id=”old“) and assigns this text to the heading-element (id=”new“).

Output

Graphical user interface, text, application Description automatically generated

The above output shows that the text of paragraph (id= “old”) is set to a heading (id= “new”).

Difference between innerText and innerHTML

The innerText and innerHTML may put confusion in your head. The innerText only considers the textual content whereas the innerHTML functions on the HTML content of an element which may include the tags as well. This section provides the difference between innerText and innerHTML by using the following code.

HTML

<p id="text"> Welcome to <strong> LinuxHint </strong> </p>

<button onclick="text()"> for innerText </button>

<button onclick="html()"> for innerHTML </button>

The above code creates a paragraph tag and two buttons. The first button triggers the text() function whereas the second function executes the html() function.

JavaScript

functiontext(){
 alert(document.getElementById("text").innerText);
}
functionhtml(){
alert(document.getElementById("text").innerHTML);
}

Two functions are created that practice the innerText and innerHTML properties on a paragraph id=”text“.

Text Description automatically generated with medium confidence

Output

Graphical user interface, text Description automatically generated with medium confidence

It is observed that the innerHTML shows the inner elements whereas the innerText has only retrieved the textual content.

Conclusion

The innerText property of JavaScript allows you to get or update/set the content of an HTML element. This article demonstrates various syntaxes of the innerText property of JavaScript with examples that better convey the concept. By going through the article, you would have learned to get the text, update the text, or set a text to an HTML element using the innerText property of JavaScript. Lastly, we have presented the difference between innerText and innerHTML to pave the concept in your head.

About the author

Adnan Shabbir