I am trying to use max-length in a textarea with jQuery Mobile, and I have run into a strange problem - the problem is that with or without the return false used in javascript, the textarea stops me from entering text anywhere from 15 to 60 characters too early..
The maxLength property is set to 1150, and I am able to type between 1090 and 1115 characters. I don't know if it is a function of using copy and paste, as this is the only way I have tested it, but it seems weird.
Here is the code:
$(document).delegate('#main','pageinit',function() {
$('#title,#description').keypress,function(e){
var cur=$(this).val().length,
max=$(this).prop('maxLength'), rem=max-cur,
ch=$(this).parents('.ui-bar').find('.char-rem'),
red=$(this).parents('.ui-bar').find('.char-rem.red'),
green=$(this).parents('.ui-bar').find('.char-rem.green');
ch.children('span').html(rem);
if(rem <= 10)
{
green.removeClass('green')
ch.addClass('red');
} else {
red.removeClass('red')
ch.addClass('green')
}
if(rem == 0)
{
if(e.which!= 8)
{
return false;
}
}
})
})
What happens every time, is that I get down to some low amount of characters remaining - but more than zero - and I can't type anymore. The same thing happens if I remove the following:
if(rem == 0)
{
if(e.which!= 8)
{
return false;
}
}
I have tried this with keydown and keyup as well, and inside or outside pageinit, and made various other adjustments, and I have verified the number of characters in the textarea with the textarea textLength property, and each time the result is the same.
Right now, I am using Chromium to test this, and I haven't tested it elsewhere, but I want to see if there is something I am just missing.
Any clues?