A basic jQuery plugin that uses a simple, overridable method to display the character count.
- Include the file as you typically would in your asset pipeline, via a
<script>
tag, etc. - Add the count container element. Example:
<div class="character-counter" data-count-target="#target-to-count"></div>
- Make sure the element specified in
data-count-target
attribute has amaxlength
attribute set - Initialize the plugin:
$('.character-counter').characterCounter();
There are three overridable options:
- maxLength: sets the default maximum length for the character counter, in case the maxlength attribute is not set on the target
- target: sets the target input element to count characters on. can be set as an option override if you are too lazy to set the data attribute
- template: sets the output of count. default:
"<strong>{{cnt}}</strong> of {{len}} characters allowed"
- {{cnt}} signifies the current count of the string
- {{len}} specifies the maximum length the string can be