Home >Web Front-end >JS Tutorial >jQuery implements a method to limit the number of characters entered into a textarea text box_jquery

jQuery implements a method to limit the number of characters entered into a textarea text box_jquery

WBOY
WBOYOriginal
2016-05-16 15:57:331271browse

The example in this article describes how jQuery implements the method of limiting the number of characters input into the textarea text box. Share it with everyone for your reference. The specific implementation method is as follows:

(function($) {
  $.fn.extend( {
    limiter: function(limit, elem) {
      $(this).on("keyup focus", function() {
        setCount(this, elem);
      });
      function setCount(src, elem) {
        var chars = src.value.length;
        if (chars > limit) {
          src.value = src.value.substr(0, limit);
          chars = limit;
        }
        elem.html( limit - chars );
      }
      setCount($(this)[0], elem);
    }
  });
})(jQuery);
 
//To setup the limiter, simply include a call similar to the one below:
var elem = $("#chars");
$("#text").limiter(100, elem);

I hope this article will be helpful to everyone’s jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn