I'm tring to prevent users entering any more input into a textarea when the char count reaches a certain value.
My code works fine in Chrome, Safarai and Firefox.
But the preventDefault method will not work for me in any version of IE.
I have found many similar topics on SO about this and included the most common suggestion:
- Performing a check to make sure preventDefault exists. If it doesn not exist use IEs returnValue=false. The problem is returnValue=false will not work either.
Any suggestions?
// Attach textarea char counter to all textarea's with class of 'textarea-char-count'
$('.textarea-char-count').live("keyup", function(event) {
MYAPP.COMMON.textareaCounter(this, event);
});
// Attach textarea char counter to element
MYAPP.COMMON.textareaCounter = function(element, evt) {
//Get char limit
var limit = $(element).attr("maxlength");
// get current character count
var current = $(element).val().length;
// if current character count is greater than or equal to the character limit
if(current >= limit) {
// prevent further characters from being entered apart from the
// Delete and Backspace keys
if(evt.which != 0 && evt.which != 8)
{
if (evt.preventDefault) {
evt.preventDefault();
}
else {
evt.returnValue = false;
}
}
}
// add the remaining character count to our span
$(element).parent().next().show().text( current + " / " + limit);
}