JQuery Text Counter Limiter

Script counter limiter menggunakan jquery untuk membatasi jumlah karakter di textfield.

(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);

kemudian tambahkan sesuai dengan elemennya

var elem = $("#chars");
$("#text").limiter(100, elem);

Hasilnya, silahkan cek disini http://sandbox.scriptiny.com/tinylimiter/
Sumber: http://www.scriptiny.com/2012/09/jquery-input-textarea-limiter/

Leave a Reply

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



*