Sha256: f3a78940ad5b851c1b7b79d86a5d5d9bdf0dc15e0e58e2a58b3eb57d845fbd37
Contents?: true
Size: 1.56 KB
Versions: 9
Compression:
Stored size: 1.56 KB
Contents
(function ($) { $.fn.characterCounter = function(){ return this.each(function(){ var itHasLengthAttribute = $(this).attr('length') !== undefined; if(itHasLengthAttribute){ $(this).on('input', updateCounter); $(this).on('focus', updateCounter); $(this).on('blur', removeCounterElement); addCounterElement($(this)); } }); }; function updateCounter(){ var maxLength = +$(this).attr('length'), actualLength = +$(this).val().length, isValidLength = actualLength <= maxLength; $(this).parent().find('span[class="character-counter"]') .html( actualLength + '/' + maxLength); addInputStyle(isValidLength, $(this)); } function addCounterElement($input){ var $counterElement = $('<span/>') .addClass('character-counter') .css('float','right') .css('font-size','12px') .css('height', 1); $input.parent().append($counterElement); } function removeCounterElement(){ $(this).parent().find('span[class="character-counter"]').html(''); } function addInputStyle(isValidLength, $input){ var inputHasInvalidClass = $input.hasClass('invalid'); if (isValidLength && inputHasInvalidClass) { $input.removeClass('invalid'); } else if(!isValidLength && !inputHasInvalidClass){ $input.removeClass('valid'); $input.addClass('invalid'); } } $(document).ready(function(){ $('input, textarea').characterCounter(); }); }( jQuery ));
Version data entries
9 entries across 9 versions & 2 rubygems