This article describes how can we implement textarea characters count that can also work during cut/copy and paste as well as you can also select characters upto desired length or can show reverse counting (characters remaining) in textarea. There are many tutorials online but they seem to make it more complex than it needs to be. Just a few lines of jQuery does the trick.
VIEW DEMO
Textarea characters count
First of all we need to include jQuery library in our page. If you don’t have already included one, You can use Google hosted library or jQuery repository or download & place at your server and add path to your page.
1 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> |
We have a textarea input in our page with id ‘ta_count‘
1 2 | <textarea id="ta_count" name="ta_count" cols="20" rows="8"></textarea> <div id="char_count"></div> |
Before the end of body we need to place a jquery function that will handle ‘propertychange‘ event as follow:
1 2 3 4 5 6 7 | <script type="text/javascript"> $('#ta_count').on('input propertychange', function() { // var max_len = 250; var len = $(this).val().trim().length; $('#char_count').text(len > 0 ? len + ' character' + (len == 1 ? '' : 's') : ''); }); </script> |
Now all is done. The code above will show like 1 character
or 5 characters
to div below the textarea.
VIEW DEMO
You can manipulate or improve the code in many ways. For example:
To show characters remaining, you can un-comment line #3 and modify code in following way below:
1 2 3 4 | var max_len = 250; var len = $(this).val().trim().length; len = max_len -len; $('#rev_char_count').text(len > 0 ? (len + ' character' + (len == 1 ? '' : 's') + ' remaining.') : ''); |
To limit and set textarea user input to max allowed characters:
$(this).val($(this).val().substring(0, max_len));
Or change color of counter, take advantage of jQuery class functions and add before closing of function :
1 2 3 4 | if(len < 4 || len > 250) $('#char_count').removeClass('green').addClass('red'); else $('#char_count').removeClass('red').addClass('green'); |
This is all about implementing textarea characters count and with a minimal change in code, you can also show remaining characters count or can change colour of counter or limit the user input by selecting only maximum allowed characters.
Complete code of our demo is as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <form class="pure-form"> <div class="pure-g"> <div class="pure-u-1 pure-u-md-1-2"> <textarea id="t1" style="height: 100px; margin-right: 1em;"></textarea> <small id="char_count"></small> </div> <div class="pure-u-1 pure-u-md-1-2"> <textarea id="t2" style="height: 100px; margin-left: 1em;"></textarea> <small id="rev_char_count"></small> </div> </div> </form> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script type="text/javascript"> $('textarea').on('input propertychange', function() { var ctrl =$(this), max_len = 100, len = $(ctrl).val().trim().length; if($(ctrl).attr('id') == 't1') { var c = $(ctrl).parent().find('#char_count'); c.text(len > 0 ? len + ' character' + (len == 1 ? '' : 's') : ''); if(len < 4 || len > max_len) c.removeClass('green').addClass('red'); else c.removeClass('red').addClass('green'); } else { var c = $(ctrl).parent().find('#rev_char_count'); len = max_len -len; c.text(len > 0 ? (len + ' character' + (len == 1 ? '' : 's') + ' remaining.') : ''); $(ctrl).val($(ctrl).val().substring(0, max_len)); } }); </script> |
Again one amazing tutorial
thanks it worked
Please post some effective tips to increase traffic on blog
Hi,
We will sure try to present with some solid tricks. An indirect article for this is here: http://fellowtuts.com/informative/how-to-speed-up-page-rendering/