Submit your widget

Twitter-like dynamic character count for textareas jQuery

Created 10 years ago   Views 10333   downloads 2243    Author n/a
Twitter-like dynamic character count for textareas jQuery
View DemoDownload 19.0 KB
Share |

The best way to explain what this plugin does is to mention Twitter. Twitter posts are limited to 140 characters. While typing the Twitter post there is this always present information about how many characters the users have before reaching the limit. The information is not only provided merely by displaying a number, there are different colors applied to certain stages to notify the user about the status.
On your site you might have a comment box or contact form fields limited to certain character count. You can use this simple plugin to provide user with that useful information.


How it works

The first thing that this plugin do is create a sibling element (it adds is immediately AFTER the form element), the "counter", where the remaining character info is stored. On each key up event or text field value change the counting function is triggered and the contents of this "counter" element is changed accordingly. If the remaining character count reaches the "warning" zone (gets close to zero) the CSS class is added. We use this class to change the color of the character count info. If the count reaches zero and goes beyond it another class is added so we can use another style for exceeding the limit.

Just so you can find your way around it, this is the code that the plugin generates by default:

<span class="counter">140</span>


Plugin Options (and default values)

limit: 140

The character limit you wish to set for your text area or input field. It must be a number.

warning: 25

When remaining characters reach the number set with this option the "warning" css class name will be applied to the counter element.

counterElement: 'span'

The type of element you wish to choose as your "counter" element. By default it is a SPAN element, but you can use paragraphs, divs, strongs, ems...

css: 'counter'

Class name added to the counter element. Use this class name as a css selector to describe element's appearance.

cssWarning: 'warning'

Class name added to the counter element once the character count reaches the "warning" number.

cssExceeded: 'exceeded'

Class name added to the counter element once the character count reaches zero.

counterText: ''

If you wish to add some text before the remaining character number, you can do so by using this option. It is empty by default.

Here's what default usage code looks like:



...and this is the plugin usage with some customized options:

    allowed: 50,  
    warning: 20,
    counterText: 'Characters left: ' 


Also take a look at the CSS I used for my demos:

form .counter{
form .warning{color:#600;} 
form .exceeded{color:#e00;}


Since I am (always) wrapping the label and form element into a div, I positioned the counter absolutely in relation to the containing div.

You can visit the demo page to see this plugin in action or you can download the plugin package. Let us know your thoughts.