Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InnerText alternative in mozilla [duplicate]

Does any one know innerText alternative of a span in mozilla? My span is

<span id='cell1'></span>  

and the javascript is

document.getElementById('cell1').innerText = 'Tenelol';

But Mozilla is not supporting this!!

like image 475
Varada Avatar asked May 16 '11 09:05

Varada


People also ask

Is textContent better than innerText?

textContent gets the content of all elements, including <script> and <style> 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.

Does Firefox support innerText?

javascript - 'innerText' works in IE, but not in Firefox - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

What is the difference between innerHTML and innerText?

The Differences Between The innerHTML property returns: The text content of the element, including all spacing and inner HTML tags. 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.

Is textContent faster than innerHTML?

Note: Unlike innerHTML, textContent has better performance because its value is not parsed as HTML. For that reason, using textContent can also prevent Cross-Site Scripting (XSS) attacks. Unlike innerText, textContent isn't aware of CSS styling and will not trigger a reflow.


1 Answers

innerText is a proprietary IE thing. The W3C defines textContent as the official property.

An easy way is to exploit the || logical operator and its short circuiting nature, as well as JavaScript returning the last evaluated value in a condition (most times the truthy operand).

var body = document.body,
    text = body.textContent || body.innerText;

jsFiddle.

(Note in the fiddle I checked for the innerText first. This was only because most people on here do not use IE. IRL, check for textContent first, and fallback to innerText.)

like image 83
alex Avatar answered Oct 03 '22 13:10

alex