InnerText alternative in mozilla [duplicate]
Asked Answered
P

1

10

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!!

Proboscidean answered 16/5, 2011 at 9:29 Comment(0)
L
13

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.)

Lazybones answered 16/5, 2011 at 9:30 Comment(5)
And since textContent is not supported by IE you'll have to use both.Goudy
for more reference help.dottoro.com/ljermebq.phpTomkin
@Parusa The fiddle may not be clear enough, I'll place the code inline :)Lazybones
textContent and innerText can give you different results so it's not recommended to use both of them in or. kellegous.com/j/2013/02/27/innertext-vs-textcontentArronarrondissement
@Arronarrondissement It depends on what you're going to do with it. In some cases, further massaging may be required.Lazybones

© 2022 - 2024 — McMap. All rights reserved.