From aa347fdd7753d25ee6b3ffb15766d351f066f9da Mon Sep 17 00:00:00 2001 From: James Skemp <strivinglife@gmail.com> Date: Wed, 21 Oct 2015 09:09:32 -0500 Subject: [PATCH] Support for per-field limits If adding a counter to multiple fields, with each having a different maxlength, the limit on the last counter on the page will be used for all other counters. This stores the limit on the respective counter instead. --- jquery.charactercounter.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/jquery.charactercounter.js b/jquery.charactercounter.js index 7f5dc10..427c20f 100644 --- a/jquery.charactercounter.js +++ b/jquery.charactercounter.js @@ -1,5 +1,5 @@ /** - * Character Counter v1.5.1 + * Character Counter v1.5.2 * ====================== * * Character Counter is a simple, Twitter style character counter. @@ -69,16 +69,16 @@ delete options.customFields['class']; } - return '<'+ options.counterWrapper +customFields(options.customFields)+' class="' + classString + '"></'+ options.counterWrapper +'>'; + return '<'+ options.counterWrapper +customFields(options.customFields)+' class="' + classString + '"' + ' data-limit="' + options.limit + '"' + '></'+ options.counterWrapper +'>'; } - function renderText(count) + function renderText(count, limit) { var rendered_count = options.counterFormat.replace(/%1/, count); if ( options.renderTotal ) { - rendered_count += '/'+ options.limit; + rendered_count += '/'+ (limit !== undefined ? limit : options.limit); } return rendered_count; @@ -88,13 +88,14 @@ { var characterCount = $(element).val().length; var counter = options.counterSelector ? $(options.counterSelector) : $(element).nextAll("." + options.counterCssClass).first(); - var remaining = options.limit - characterCount; + var counterLimit = counter.attr('data-limit'); + var remaining = counterLimit - characterCount; var condition = remaining < 0; if ( options.increaseCounting ) { remaining = characterCount; - condition = remaining > options.limit; + condition = remaining > counterLimit; } if ( condition ) @@ -112,7 +113,7 @@ } } - counter.html(renderText(remaining)); + counter.html(renderText(remaining, counterLimit)); } function bindEvents(element)