This free small JavaScript tutorial will overview the differences between innerText and textContent attributes, so you can understand and use them better.
- Demo
- Enlarge
- Reload
- New window
Free iPage Web Hosting for First Year NOW
If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?
Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
I was having an issue with the javascript innerText property today. I was to lazy to convert the legacy code that I was working on to use proper DOM so I did some reading on the issue.
Turns out that FireFox doesn't use innerText at all. Apparently the proper DOM property to use is textContent. textContent does basically the same thing as innerText.
A not so pretty solution to this issue is to check at the start of the script whether the browser supports innerText or textContent. I found a few resources that allowed me to copy and paste a few lines to make everything work.
This one which I found on coderlab
is a little bulky but it gets the job done. It checks which property is
available and then forces the programmer to check that flag every time
he wants to use the property.
var hasInnerText =
(document.getElementsByTagName("body")[0].innerText
!= undefined) ? true : false;
var elem = document.getElementById('id');
if(!hasInnerText){
elem.textContent = value;
} else{
elem.innerText = value;
}
This is the method I actually used. I just or'd the two different
properties together and it seemed to work. I guess one will always be
undefined.
var message = elem.innerText || elem.textContent;
At least FireFox is following the standards unlike Internet Explorer which always needs special css and javascript. Making everything work in Internet Explorer always adds hours onto development time...
- Sent (0)
- New
Open tool hub for free to use by any one for every one with hundreds of tools
chatGPTaz
Voice chat comning soon. You could talk to ChatGPT and hear the most intelligent AI speaks very soon