jquery solves textarea not support maxlength approach

Source: Internet
Author: User
Tags bind

Because IE does not support textarea maxlength properties, the test in Firefox 6 shows that Firefox supports this attribute. So it's easy. Write yourself a jquery extension so it's easy to implement the maximum length for textarea control.

(function ($) {
$.fn.textarealimit = function (settings) {
Settings = Jquery.extend ({
length:1000
}, settings);
MaxLength =settings.length;
$ (this). attr ("MaxLength", maxlength). Bind ("KeyDown", Dokeydown). Bind ("KeyPress", dokeypress). Bind ("Beforepaste", Dobeforepaste). Bind ("Paste", dopaste);
function dokeypress ()
{
var OTR = Document.selection.createRange ()
if (oTR.text.length >= 1)
Event.returnvalue = True
else if (This.value.length > MaxLength-1)
Event.returnvalue = False
}
function Dokeydown ()
{
var _obj=this;
settimeout (function ()
{
if (_obj.value.length > MaxLength-1)
{
var OTR = Window.document.selection.createRange ()
Otr.movestart ("character", -1* (_obj.value.length-maxlength))
Otr.text = ""
}
},1)
}
function Dobeforepaste ()
{
Event.returnvalue = False
}
function Dopaste ()
{
Event.returnvalue = False
var OTR = Document.selection.createRange ()
var iinsertlength = maxlength-this.value.length + oTR.text.length
var sData = window.clipboardData.getData ("Text"). substr (0, Iinsertlength)
Otr.text = SData;
}
}
}) (JQuery);

Above only for IE control copy and paste control and input control, because of the use of IE characteristics, these methods in Firefox is ineffective.
Calling code:

$ (document). Ready (function () {
$ ("#ctl00_ZiiOContent_ucQuestionnaire_txtquestion4_2"). Textarealimit ();
});

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.