Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

innerText vs textContent - getting some errors on Firefox [duplicate]

I'd getting some errors on my scripts here, in the first one I've declared a var text(); type to create a box with some text content.

var x=$('#x').text();

In my second .js I'm doing some handling on this var that ensure my text i'll have less char than max-length, here is:

var limitMaxLength = function(element) {
   var maxLength = $(element).data("max-length");
      if (maxLength) {
         $(element).on("keypress", function(event){
            if (event.target.innerText.length > maxLength || event.keyCode === 13) {
               event.preventDefault();
            }
      });
   }
};

So I having some issues on Firefox getting this error: [TypeError: event.target.innerText is undefined]

In IE, Opera, Chrome and Safari my code works fine, i'm getting error only on Firefox. *Sorry for my bad english.

like image 460
brunoluizgr Avatar asked Mar 28 '26 21:03

brunoluizgr


1 Answers

innerText was a Microsoft Creation, left over from the days of old. Firefox decided not to support it in favour of the w3 compliant, textContent. However, IE8 & below do not support textContent, therefore, if you wish to support Legacy browsers, you're in a bit of a bind.

var text = elem.textContent || elem.innerText;

or

elem.innerHTML // eww, but it works, eh?

or, just ditch IE8 (easier said than done, especially at work)

like image 130
ndugger Avatar answered Apr 02 '26 02:04

ndugger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!