textarea limit input length

The maxlength attribute specifies the maximum length (in characters) of the text area;

Internet Explorer 10, Firefox, Chrome, and Safari support the maxlength property.

NOTE: The maxlength property is not supported in Internet Explorer 9 and earlier or Opera.

In order to meet the requirement that textarea can be used in low version browsers such as IE8/9 and can limit the input length, it is summarized as follows:

HTML:

<textarea id="area" maxlength="200"></textarea>

 JS:

$("#area").on("input propertychange", function() {
    var $this = $(this),
        _val = $this.val();
    if (_val.length > 200) {
        $this.val(_val.substring(0, 200));
    }
});

 Combined with maxlength="", double insurance!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326330527&siteId=291194637
Recommended