I am using this script to limit the characters on lines 1-3 on my textarea. It works in Firefox and Chrome. But in IE8, it shows an error: "Object doesn't support this property or method" on the line that uses filter() method.
Here's the code:
var result = jQuery('#result');
var my_textarea = jQuery('#mytext');
my_textarea.on('keyup', function(event){
var el = jQuery(this);
var lines = el.val().split('\n').length;
var chars = el.val().split('').filter(function(v){
return v != '\n';
}).length;
result.html('You have ' + lines + ' lines and ' + chars + ' chars');
if ((lines === 1 && chars > 20) || (lines === 2 && chars > 40) || (lines === 3 && chars > 60)) {
my_textarea.val( my_textarea.val() + "\n");
}
});
How do I resolve this?
I am using this script to limit the characters on lines 1-3 on my textarea. It works in Firefox and Chrome. But in IE8, it shows an error: "Object doesn't support this property or method" on the line that uses filter() method.
Here's the code:
var result = jQuery('#result');
var my_textarea = jQuery('#mytext');
my_textarea.on('keyup', function(event){
var el = jQuery(this);
var lines = el.val().split('\n').length;
var chars = el.val().split('').filter(function(v){
return v != '\n';
}).length;
result.html('You have ' + lines + ' lines and ' + chars + ' chars');
if ((lines === 1 && chars > 20) || (lines === 2 && chars > 40) || (lines === 3 && chars > 60)) {
my_textarea.val( my_textarea.val() + "\n");
}
});
How do I resolve this?
Share Improve this question asked Feb 1, 2012 at 2:30 catandmousecatandmouse 11.8k24 gold badges95 silver badges157 bronze badges3 Answers
Reset to default 10IE8 doesn't support the Array.filter method.
MDN has a replacement.
filter
(and several other methods on Array
) are relatively new and aren't implemented in older browsers - not just IE8, but older Firefox too.
You may be interested in array_filter
from PHPJS, as this produces the same effect.
Then again, looking at your code, wouldn't this be simpler?
var chars = el.val().replace(/\n/g,'').length;
Array.filter js polyfill:
[].filter||(Array.prototype.filter=function(g,f,j,i,h){
j=this;i=[];
for(h in j){~~h+""==h&&h>=0&&g.call(f,j[h],+h,j)&&i.push(j[h])}
return i
});
Jakob E