How to get the textarea’s maxlength attribute to work in Internet Explorer

If you are developing for an older version of Internet Explorer (IE8 and older), you might have noticed that your chronogically-challenged browser does not support the maxlength attribute. Fortunately, you can solve it with a few lines of jQuery:

//textarea maxlength support for chronogically-challenged browsers
$('textarea[maxlength]').keyup(function(){
    //Get the value
    var text = $(this).val();
    //Get the maxlength
    var limit = $(this).attr('maxlength');
    //Check if the length exceeds what is permitted
    if(text.length > limit){
        //Truncate the text if necessary
        $(this).val(text.substr(0, limit));  
    }
});

This snipped of code will apply the fix to all textareas in your page.

3 comments on “How to get the textarea’s maxlength attribute to work in Internet Explorer

  1. I see a lot of interesting posts on your blog.
    You have to spend a lot of time writing, i know how to save you a lot of
    work, there is a tool that creates unique, SEO friendly posts in couple of seconds, just search in google – k2 unlimited content

  2. hey i used your above code but its not working in mine it is giving error like maxlength attribute cant be used inside textarea field.

    please help

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax