what does the innertext give you?
When you read or set innerText on an element, you are working with the
rendered, visible text inside that element, not its raw HTML.
Quick Scoop: What innerText actually gives you
At a high level:
- It returns the text content of an element as a human would see it on the page.
- It respects layout and styling that affect visibility (like
display: none).
- It ignores tags and some non-visible nodes like
<script>and<style>.
So if you imagine selecting an element with your mouse and copying it,
innerText is roughly what would go onto the clipboard.
Key behaviors in simple terms
1. Only visible text
- Text inside elements hidden with CSS (for example
display: none) is not included ininnerText.
- Text that is off-screen but still considered rendered (like visually hidden with some techniques) can still be included.
Example idea:
- HTML has
Hello <span style="display:none">Hidden</span> World. element.innerTextâ"Hello World"(no âHiddenâ).
2. Formatting and line breaks
innerTextinserts line breaks in ways that roughly follow the visual layout: block elements tend to become new lines.
- It normalizes whitespace instead of preserving every single space and newline from the original HTML source.
This is why innerText often feels like âcopied text from the screen,â not
raw source text.
3. How it differs from innerHTML and textContent
You can think of the three main DOM properties like this:
| Property | What it gives you |
|---|---|
innerHTML | The HTML markup inside the element, including tags and attributes, exactly as HTML. | [3][5]
innerText | Rendered, visible text only, with some formatting (line breaks), ignoring hidden text and most non-visible details. | [1][9][5][3]
textContent | All text nodes inside the element, including hidden ones, but no HTML tags and with minimal formatting awareness. | [9][5][3]
- Use
innerTextwhen you care about what a user sees on the page.
- Use
innerHTMLwhen you care about the HTML structure and tags.
- Use
textContentwhen you want all the plain text , visible or not, without markup.
4. Getting vs. setting with innerText
innerText is both a getter and a setter:
- Get :
element.innerTextâ returns a string of visible text as discussed.
- Set :
element.innerText = "New text"â removes all child nodes and replaces them with that text, turning line breaks in the string into appropriate DOM/text layout.
When you set innerText, you are not inserting HTML; any <b>, <span>,
etc. are treated as plain text characters.
5. Where this shows up in âlatestâ tutorials and forum talk
Recent blog posts and dev articles still describe innerText as the âvisible
textâ option in the trio with innerHTML and textContent, often showing
examples where hidden elements are excluded from innerText but included in
textContent.
Forum questions from learners commonly revolve around trying to understand why
innerText doesnât show text they expect (usually because itâs hidden or
because they actually needed innerHTML or textContent).
Youâll often see advice like: âUseinnerText if you want whatâs visible; use
innerHTML if youâre manipulating HTML; use textContent for raw text
including hidden content.â
TL;DR
innerTextgives you the human-visible, rendered text of an element, with layout-aware line breaks and no HTML tags.
- It skips text that is hidden via styling and ignores
<script>/<style>content.
- Think of it as: âWhat would show up if I highlighted this element in the browser and copied it?â
Information gathered from public forums or data available on the internet and portrayed here.