jQuery plugin: word-counter for textarea
- Monday, October 20, 2008, 11:43
- how-to, javascript, jquery, tutorial
- 24 comments
Sorry friends on the way of move to new home I’ve been lost from the blog as I didn’t have internet connection for few last weeks. Now, I’m back and I try to be regular as much as I can. Inspired from the new feature of wordpress 2.6.x which displays word count of each post, I’ve developed jQuery plugin to display word-count of Textarea. Please note that it is word count plugin not character counter.
LIVE DEMO
Jquery plugin: Word-count code
jQuery.fn.wordCount = function(params)
{
var p = {
counterElement:"display_count"
};
var total_words;
if(params) {
jQuery.extend(p, params);
}
//for each keypress function on text areas
this.keypress(function()
{
total_words=this.value.split(/[\s\.\?]+/).length;
jQuery('#'+p.counterElement).html(total_words);
});
};
As you can in the above code, I assume that each words of a paragraph are separated by the either spaces or dots(.) . Please have your suggestion if i can more characters to improve this plugin.
How to use this plugin?
Well, you can guess that a Textarea is needed of whose words are counted and another element like div or span needed to display the word count.
For example, the below HTML code contains Textarea (whose total word is counted) and span (for displaying counted word).
<textarea name="word_count" id="word_count" cols="30" rows="6"></textarea> Total word Count : <span id="display_count">0</span>
Remember that the id of text area is “word_count” and “display_count“.
Now, let look at the jQuery code for displaying word count of text area , note that we are calling the function wordCount() which we’ve just developed in the plugin of jQuery.
$('#word_count').wordCount();
Note: display_count is default id of element which is already defined in the plugin if you want to use the different id of elment then you’ve to override the value of variable counterElement which contains the id of element displaying word count.
$('#word_count').wordCount({counterElement:"word_counter"});
Popularity: 11%
Related Posts
» How to execute PHP code entered from textbox or textarea
» Making Money from blog with the help of WP Affiliate Elite
» Display random number in random way using JavaScript
» Top Floating message box using jQuery






nice how to, very usefull, but in the article I think you may want to say wordpress 2.6.x right?
best regards
@Davi- Thanks for heads up dude….corrected now…
Another nice tutorial, this is a useful piece of code for any website allowing comments from the public.
Thanks
simple and superb. nice one. this is what i need. thanks.
This is the nice think, i have used it but i have found little error in it and i hope that you will fix it, the problem is that i have typed “How are you ?” you see there is space before question mark it will give number of words 4 but if i remove the space between word “you” and the question mark (?) the still it will give number of count to 4. which i think is wrong. hope you will fix that problem.
I especially use word counter for counting the no of words of my the Sponsored posts from payperpost and sponsored reviews.
I think Blogger doesn’t support jquery plugin.If it would have in JavaScript then it wound have become really useful to me.
Roshan dai,
I am looking for a way to use tinymce with character counter (limit character). I tried with jquery character count also, but didn’t work. Can you give me any idea?
@Deineshd - you can easily use this if you can use javascript, jquery and jquery plugin is just a javascript framework and piece of code of javascript… if you can use javascript then you must be able to use jquery
@shakeel - javascript code must work with tinymce as it is also built using javascript
@Asim Sajjad - thanks for reporting the bug, I’ll fix it in the next update of this plugin..
Roshan…if you write “la-vida-loca/es@loca” count 1 word. If you write “la.vida.loca,es.loca”, count 4 words. The separator is just a dot or space when they should be (space).,;-@
Realy good Roshan!!
@felix - thanks very much for your valuable suggestion, I’ll get it fixed in next release…
Roshan, if i type “hello,how” then it will count 1 which is i think wrong, i think you should handle the comma (,) sign as well hope you will do it in the future release of the plug-in if you like my suggestion
@asim - every suggestion is highly appreciated and will be used in the next version…
when will you give the new version of the word counter plug-in, i have write the code for that plug-in and i have tested it well, if you don’t mind i will share on you blag ? can i do or not ?
Track back from: http://webdevvote.com
Nice plugin..
Just wanted to highlight a bug. This does’nt work is I copy / paste certain text in the textarea. I think this is because you have mapped keypress event of the textarea. You have to check .onpaste event too.
Nice effort and a simply jquery code.
I like your site and I visit you frequently
@Viral Patel - thanks a lot for the suggestion … I’ll be working on the the fixes mentioned by you and other friends in the another release of this plugin…
please see this hope this will help you
http://asimsajjad.blogspot.com/2008/11/jquery-word-counter.html
very good article
@waqas - thanks for revising my code…..
I have not revised your code i have just read that post and from there i have check your post as the person name Asim has give your link
Thanks for your code.
GOOD ONE! but how can we limit user to for example 100 words?
Hi Roshan,
Thanks for this code. It is exactly what i’m looking for. I also needed a way to limit to a specific number of words, so I added this to my code… Hope it is useful to someone:
Inside the wordCount function at the “this.keypress” sub-function, I modified the code:
if (total_words > p.wordLimit) {
jQuery(’#'+p.counterElement).html(p.wordLimit + ‘ word maximum reached’);
} else {
jQuery(’#'+p.counterElement).html(total_words);
}
Next, in the document.ready area, I call the function and pass in a new variable for wordLimit:
$(’#word_count’).wordCount({counterElement:”word_counter, wordLimit:50 “});
It’s not the most efficient way to do it, but you get the idea.
Thanks again,
Keith