Top
Capture the Text of an Element using innerText()
  • 05 Sep 2024
  • 1 Minute To Read
  • Dark
    Light
  • PDF

Capture the Text of an Element using innerText()

  • Dark
    Light
  • PDF

Article summary

What is innerText and textContent?

In JavaScript, innerText and textContent are two properties that are used to access and modify the text content of an element on a web page. By default, when you select an element using Whatfix, the element text is captured using textContent().

Here are some key differences between the two:

innerText textContent
Retrieves the visible text content of an element. Retrieves the content of an element, including all text and nested elements.
Excludes text that is hidden with CSS or rendered invisible due to its parent’s visibility settings. Ignores the style or visibility of the element.
Returns the formatted and styled text content. Returns the raw text content as a string.

For example, consider the following code snippet:

<div id="test">
  Hello <span style="display: none;">world</span>!
</div>

let element = document.getElementById("example");
console.log(element.textContent); // Output: Hello world!
console.log(element.innerText); // Output: Hello !

In this snippet, the element with the ID test contains a hidden element with the text world. Here's how the output varies:

  • element.textContent outputs “Hello world!” (including the hidden text).
  • element.innerText outputs “Hello!” (excluding the hidden text).

Why should I override Text Content with Inner Text?

When Override Text Content with Inner text is enabled, instead of capturing the textContent property of selected elements, Whatfix captures the innerText property. Here’s why:

  • Visibility: By using innerText, only the text content visible to the user on the screen is captured.
  • Hidden Elements: Certain text elements may be hidden or styled in a way that makes them invisible to the user. Despite this, they are still captured by the textContent property. Using innerText ensures accuracy.

Use the following steps to override textContent with innerText:

  1. On the Whatfix Guidance dashboard, click Settings.
    ia_left_nav_settings

  2. Click Advanced customisation.
    ia_settings_advanced%20configuration

  3. Click Technical configuration.
    ac_tech config.png

  4. Click Add preferences.
    tech config_add prefrences.png

  1. Click Common Properties.
    tech config_common properties.png

  2. Select the Override text content with inner text checkbox.
    tech config_enable override text content .png

  3. Click Add.
    tech config_add button.png

  4. Enable the Override text content with inner text toggle.
    tech config_override text content toggle.png

  5. Click Save.
    tech config_save.png


Was this article helpful?

Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.