
398 views
Javascript – innerText
The JavaScriptinnerText
property is used to get or set the text content of an element, excluding the HTML tags inside it. It returns the visible text contained within an element, including its descendants.
For example, consider the following HTML code:
HTML<span role="button" tabindex="0" data-code="<div id="myDiv">
<p>This is some text.</p>
<p>This is some more text.</p>
<div id="myDiv">
<p>This is some text.</p>
<p>This is some more text.</p>
</div>
To get the text content of the myDiv
element using innerText
, you can use the following JavaScript code:
JavaScript
const myDiv = document.getElementById("myDiv");
const text = myDiv.innerText;
console.log(text); // Output: "This is some text. This is some more text.."
Similarly, to set the text content of an element using innerText
, you can simply assign the new value to the property. For example:
JavaScript
const myDiv = document.getElementById("myDiv");
myDiv.innerText = "This is the new text.";
This will change the text content of the myDiv
element to “This is the new text.” and will remove the previous text.