HTML Node outerText
Property: Extracting and Manipulating Text Content
The outerText
property of an HTML Node interface is a powerful tool for accessing and modifying the text content of an element, including the text of the element itself and all its descendants. It differs from innerText
by also considering the text of the element’s tags when getting the value, or replacing the entire element when setting the value. This comprehensive guide will walk you through the usage, syntax, and practical applications of the outerText
property, providing clear examples to enhance your understanding.
What is the outerText
Property?
The outerText
property represents the serialized text content of a node, including the element’s tags, and allows you to retrieve or replace this content. When you retrieve the outerText
value, it returns a string representing the concatenated text content of the node and all its descendants. When you set the outerText
value, it replaces the entire node with the provided text, effectively removing the element and its children from the DOM.
Purpose of the outerText
Property
The primary purposes of the outerText
property are:
- Retrieving Text Content: Access the combined text content of an element and its descendants.
- Replacing Elements: Replace an entire HTML element and its content with new text.
- Manipulating DOM Structure: Dynamically modify the structure and content of a web page.
Syntax
The syntax for accessing and modifying the outerText
property is straightforward:
- Getting the
outerText
:
let textContent = node.outerText;
- Setting the
outerText
:
node.outerText = "New text content";
Important Notes
- The
outerText
property is non-standard and may not be supported by all browsers. It is recommended to usetextContent
orinnerText
for better compatibility. ⚠️ - Setting
outerText
replaces the entire element, including its tags, in the DOM. - When getting the value,
outerText
includes the text of the element’s tags, unlikeinnerText
.
Practical Examples
Let’s explore practical examples of using the outerText
property, starting with basic retrieval and modification scenarios.
Retrieving Text Content
This example demonstrates how to retrieve the outerText
of an element and display it.
<div id="outerTextDiv">
This is some <b>bold</b> text.
</div>
<button id="getTextButton">Get Outer Text</button>
<p id="displayText"></p>
<script>
const outerTextDiv_example1 = document.getElementById("outerTextDiv");
const getTextButton_example1 = document.getElementById("getTextButton");
const displayText_example1 = document.getElementById("displayText");
getTextButton_example1.addEventListener("click", function () {
const textContent = outerTextDiv_example1.outerText;
displayText_example1.textContent = "Outer Text: " + textContent;
});
</script>
Output:
When the “Get Outer Text” button is clicked, the following text will be displayed in the paragraph:
Outer Text: This is some bold text.
Replacing an Element with Text
This example shows how to replace an entire HTML element with new text using the outerText
property.
<div id="replaceDiv">
This div will be replaced.
</div>
<button id="replaceButton">Replace Element</button>
<script>
const replaceDiv_example2 = document.getElementById("replaceDiv");
const replaceButton_example2 = document.getElementById("replaceButton");
replaceButton_example2.addEventListener("click", function () {
replaceDiv_example2.outerText = "This is the new text.";
});
</script>
Output:
After clicking the “Replace Element” button, the original div
element will be removed from the DOM and replaced with the text “This is the new text.”.
Modifying Text Content
This example demonstrates how to modify the text content of an element using outerText
.
<div id="modifyDiv">
Original text content.
</div>
<button id="modifyButton">Modify Text</button>
<script>
const modifyDiv_example3 = document.getElementById("modifyDiv");
const modifyButton_example3 = document.getElementById("modifyButton");
modifyButton_example3.addEventListener("click", function () {
modifyDiv_example3.outerText = "Modified text content.";
});
</script>
Output:
After clicking the “Modify Text” button, the text content of the div
element will be changed to “Modified text content.”.
Comparing outerText
with innerText
and textContent
It’s essential to understand the differences between outerText
, innerText
, and textContent
to use them effectively.
| Property | Description |
| ———– | ————————————————————————————————————— |
| outerText
| Gets or sets the serialized text content of a node, including the element’s tags. Replaces the entire element when set. |
| innerText
| Gets or sets the text content of an element and its descendants, considering rendered text. |
| textContent
| Gets or sets the text content of a node and its descendants, including all whitespace. |
Key Differences:
outerText
is non-standard and replaces the entire element when set.innerText
considers rendered text and is more suitable for user-displayed content.textContent
retrieves all text content, including whitespace, and is generally preferred for cross-browser compatibility.
Real-World Applications
The outerText
property can be useful in specific scenarios, such as:
- Replacing elements with dynamic content: Dynamically replace parts of a webpage with updated text content.
- Modifying text in specific elements: Change the text displayed in particular sections of a page based on user interactions or data updates.
Browser Support
Due to its non-standard nature, outerText
has limited browser support. It is primarily supported by Internet Explorer and older versions of some other browsers. For modern web development, it’s recommended to use textContent
or innerText
for better compatibility. 🌐
Conclusion
The outerText
property of the HTML Node interface provides a way to access and modify the text content of an element. However, due to its non-standard nature and limited browser support, it’s advisable to use textContent
or innerText
for better compatibility and reliability in modern web development. Understanding the purpose, syntax, and practical examples of outerText
helps in making informed decisions when manipulating the DOM and managing text content in web applications.