JQuery adds the maxlength attribute for textarea and is compatible with IE

Source: Internet
Author: User

// JQuery adds maxlength for textarea
// Textarea does not support the maxlength attribute by default.
// Use the JQuery keyup event:
The JQuery code is as follows::
Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Function (){
// Textarea can also be used for IE
$ ("Textarea [maxlength]"). keyup (function (){
Var area = $ (this );
Var max = parseInt (area. attr ("maxlength"), 10); // obtain the value of maxlength
If (max> 0 ){
If (area. val (). length> max) {// The text length of textarea is greater than maxlength
Area. val (area. val (). substr (0, max); // truncate the text of textarea and assign a value again.
}
}
});
// Handling the copied characters
$ ("Textarea [maxlength]"). blur (function (){
Var area = $ (this );
Var max = parseInt (area. attr ("maxlength"), 10); // obtain the value of maxlength
If (max> 0 ){
If (area. val (). length> max) {// The text length of textarea is greater than maxlength
Area. val (area. val (). substr (0, max); // truncate the text of textarea and assign a value again.
}
}
});
});
</Script>

Html:
Copy codeThe Code is as follows:
<Textarea style = "width: 300px; height: 60px;" maxlength = "10"> </textarea>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.