I'm trying to get an element's innerText
but Chrome returns an empty string because my element is hidden.
Firefox does not.
Is there a way to retrieve the innerText
in Chrome even if the element is invisible?
console.log(document.querySelector('div').innerText + ':' + document.querySelector('div').innerHTML);
div {
height: 0;
overflow: hidden;
}
<div>
Hello, I'm <strong>empty!</strong>
</div>
Honestly I'm really surprised Chrome behaves like this, is it a bug?
https://jsfiddle.net/r8q2znc4/
In some cases, one may find it useful to get the hidden text, which can be retrieved from element's textContent , innerText or innerHTML attribute, by calling element. attribute('attributeName') . element. getAttribute("textContent") worked for me.
The innerText property returns: Just the text content of the element and all its children, without CSS hidden text spacing and tags, except <script> and <style> elements. The textContent property returns: The text content of the element and all descendaces, with spacing and CSS hidden text, but without tags.
OpenXML- InnerText is always an Integer and not a String.
The innerText property of the HTMLElement interface represents the rendered text content of a node and its descendants. As a getter, it approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the clipboard.
You want to use textContent
for this, as it returns text from hidden elements
document.querySelector('div').textContent
The documentation states that
TextContent
differs frominnerText
...Internet Explorer introduced
node.innerText
.
The intention is similar but with the following differences:While
textContent
gets the content of all elements, including<script>
and<style>
elements,innerText
does not.
innerText
is aware of style and will not return the text of hidden elements, whereastextContent
will.As
innerText
is aware of CSS styling, it will trigger a reflow, whereastextContent
will not.
This is not a Chrome bug, but rather a Firefox bug, innerText
shouldn't return the content of hidden elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With