Get text document html

Node: textContent property

The textContent property of the Node interface represents the text content of the node and its descendants.

Note: textContent and HTMLElement.innerText are easily confused, but the two properties are different in important ways.

Value

A string, or null . Its value depends on the situation:

    If the node is a document or a doctype, textContent returns null .

Note: To get all of the text and CDATA data for the whole document, use document.documentElement.textContent .

Warning: Setting textContent on a node removes all of the node’s children and replaces them with a single text node with the given string value.

Differences from innerText

Don’t get confused by the differences between Node.textContent and HTMLElement.innerText . Although the names seem similar, there are important differences:

  • textContent gets the content of all elements, including and elements. In contrast, innerText only shows «human-readable» elements.
  • textContent returns every element in the node. In contrast, innerText is aware of styling and won’t return the text of «hidden» elements.
    • Moreover, since innerText takes CSS styles into account, reading the value of innerText triggers a reflow to ensure up-to-date computed styles. (Reflows can be computationally expensive, and thus should be avoided when possible.)

    Differences from innerHTML

    Element.innerHTML returns HTML, as its name indicates. Sometimes people use innerHTML to retrieve or write text inside an element, but textContent has better performance because its value is not parsed as HTML.

    Moreover, using textContent can prevent XSS attacks.

    Examples

    Start with this HTML fragment.

    div id="divA">This is span>somespan> text!div> 

    You can use textContent to get the element’s text content:

    let text = document.getElementById("divA").textContent; // The text variable is now: 'This is some text!' 

    If you prefer to set the element’s text content, you can do:

    .getElementById("divA").textContent = "This text is different!"; // The HTML for divA is now: // 
    This text is different!

    Specifications

    Browser compatibility

    BCD tables only load in the browser

    See also

    Found a content problem with this page?

    This page was last modified on Apr 7, 2023 by MDN contributors.

    Your blueprint for a better internet.

    MDN

    Support

    Our communities

    Developers

    Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
    Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

    Источник

    Get text document html

    Last updated: Jan 12, 2023
    Reading time · 3 min

    banner

    # Get the Text of an HTML Element in JavaScript

    Use the textContent property to get the text of an HTML element, e.g. const result = element.textContent .

    The textContent property will return the text content of the element and its descendants. If the element is empty, an empty string is returned.

    Here is the HTML for the example.

    Copied!
    DOCTYPE html> html lang="en"> head> title>bobbyhadz.comtitle> meta charset="UTF-8" /> head> body> div id="container"> One, span style="background-color: salmon">Twospan>, Three div> script src="index.js"> script> body> html>

    And here is the related JavaScript code.

    Copied!
    const container = document.getElementById('container'); // 👇️ One, Two, Three console.log(container.textContent); // 👇️ One, Two, Three console.log(container.innerText);

    get text of div

    We used the textContent property to get the text content of the div and its descendants.

    If the div element were empty, the property would return an empty string.

    # Handling leading and trailing spaces when using textContent

    You might get leading or trailing spaces when using textContent depending on the structure of your HTML.

    If you need to remove any leading or trailing spaces, use the trim() method.

    Copied!
    const container = document.getElementById('container'); // 👇️ "One, Two, Three" const result = container.textContent.trim();

    The String.trim() method removes the leading and trailing whitespace from a string and returns a new string, without modifying the original string.

    The trim() method removes all whitespace characters including spaces, tabs and newlines.

    # Using textContent vs innerText

    The code snippet also showed that we can use the innerText property to get the text content of an element and its descendants.

    Copied!
    const container = document.getElementById('container'); // 👇️ One, Two, Three const result = container.innerText;

    However, there are some important differences between the textContent and innerText properties:

    1. textContent gets the content of all elements, including script and style elements, whereas innerText only gets the content of «human-readable» elements.
    2. innerText is aware of styling and does not return the text of hidden elements, whereas textContent does not take styles into consideration.
    3. using textContent can prevent cross-site scripting attacks.

    innerText takes CSS styles into account, so when the property is accessed, a reflow is triggered to ensure the styles are up-to-date.

    Reflows can be expensive and should be avoided when possible.

    When you use textContent and innerText to set the element’s text content, the element’s child nodes get removed.

    When using the textContent and innerText properties to update the text content of the element, the child nodes of the element get replaced with a single text node with the provided string value.

    If you need to set an element’s text content, you should use the insertAdjacentText method instead.

    Copied!
    const container = document.getElementById('container'); // ✅ Update the text content of the element container.insertAdjacentText('beforeend', ', Four'); // ✅ Update the HTML content of the element container.insertAdjacentHTML( 'beforeend', ', Five', );

    The insertAdjacentText method doesn’t remove the child nodes of the element it was called on.

    The insertAdjacentText method takes the following 2 parameters:

    1. position — the position relative to the element where the text should be inserted. Can be one of the following 4:
    • beforebegin — before the element itself.
    • afterbegin — just inside the element, before its first child.
    • beforeend — just inside the element, after its last child.
    • afterend — after the element itself.
    1. data — the string from which to create a new text node to insert at the given position.

    In the example, we added a string inside of the element, after its last child. However, you can pass a different first argument to the method depending on your use case.

    The example also shows how to use the insertAdjacentHTML method to insert HTML into the div element.

    The insertAdjacentHTML method takes the same first parameter as insertAdjacentText .

    Copied!
    const container = document.getElementById('container'); // ✅ Update HTML content of element container.insertAdjacentHTML( 'beforeend', ', Five', );

    However, note that you shouldn’t use user-generated input without escaping it, because that leads to a cross-site scripting vulnerability.

    # Additional Resources

    You can learn more about the related topics by checking out the following tutorials:

    I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

    Источник

    Get text document html

    Last updated: Jan 12, 2023
    Reading time · 3 min

    banner

    # Get the Text of an HTML Element in JavaScript

    Use the textContent property to get the text of an HTML element, e.g. const result = element.textContent .

    The textContent property will return the text content of the element and its descendants. If the element is empty, an empty string is returned.

    Here is the HTML for the example.

    Copied!
    DOCTYPE html> html lang="en"> head> title>bobbyhadz.comtitle> meta charset="UTF-8" /> head> body> div id="container"> One, span style="background-color: salmon">Twospan>, Three div> script src="index.js"> script> body> html>

    And here is the related JavaScript code.

    Copied!
    const container = document.getElementById('container'); // 👇️ One, Two, Three console.log(container.textContent); // 👇️ One, Two, Three console.log(container.innerText);

    get text of div

    We used the textContent property to get the text content of the div and its descendants.

    If the div element were empty, the property would return an empty string.

    # Handling leading and trailing spaces when using textContent

    You might get leading or trailing spaces when using textContent depending on the structure of your HTML.

    If you need to remove any leading or trailing spaces, use the trim() method.

    Copied!
    const container = document.getElementById('container'); // 👇️ "One, Two, Three" const result = container.textContent.trim();

    The String.trim() method removes the leading and trailing whitespace from a string and returns a new string, without modifying the original string.

    The trim() method removes all whitespace characters including spaces, tabs and newlines.

    # Using textContent vs innerText

    The code snippet also showed that we can use the innerText property to get the text content of an element and its descendants.

    Copied!
    const container = document.getElementById('container'); // 👇️ One, Two, Three const result = container.innerText;

    However, there are some important differences between the textContent and innerText properties:

    1. textContent gets the content of all elements, including script and style elements, whereas innerText only gets the content of «human-readable» elements.
    2. innerText is aware of styling and does not return the text of hidden elements, whereas textContent does not take styles into consideration.
    3. using textContent can prevent cross-site scripting attacks.

    innerText takes CSS styles into account, so when the property is accessed, a reflow is triggered to ensure the styles are up-to-date.

    Reflows can be expensive and should be avoided when possible.

    When you use textContent and innerText to set the element’s text content, the element’s child nodes get removed.

    When using the textContent and innerText properties to update the text content of the element, the child nodes of the element get replaced with a single text node with the provided string value.

    If you need to set an element’s text content, you should use the insertAdjacentText method instead.

    Copied!
    const container = document.getElementById('container'); // ✅ Update the text content of the element container.insertAdjacentText('beforeend', ', Four'); // ✅ Update the HTML content of the element container.insertAdjacentHTML( 'beforeend', ', Five', );

    The insertAdjacentText method doesn’t remove the child nodes of the element it was called on.

    The insertAdjacentText method takes the following 2 parameters:

    1. position — the position relative to the element where the text should be inserted. Can be one of the following 4:
    • beforebegin — before the element itself.
    • afterbegin — just inside the element, before its first child.
    • beforeend — just inside the element, after its last child.
    • afterend — after the element itself.
    1. data — the string from which to create a new text node to insert at the given position.

    In the example, we added a string inside of the element, after its last child. However, you can pass a different first argument to the method depending on your use case.

    The example also shows how to use the insertAdjacentHTML method to insert HTML into the div element.

    The insertAdjacentHTML method takes the same first parameter as insertAdjacentText .

    Copied!
    const container = document.getElementById('container'); // ✅ Update HTML content of element container.insertAdjacentHTML( 'beforeend', ', Five', );

    However, note that you shouldn’t use user-generated input without escaping it, because that leads to a cross-site scripting vulnerability.

    # Additional Resources

    You can learn more about the related topics by checking out the following tutorials:

    I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

    Источник

    Читайте также:  Java определить тип данных
Оцените статью