9lessons programming blog
Loading Search
Saturday, April 10, 2010

Live Character Count Meter with Jquery.

In this post I want to explain how to do live character or word count meter using Jquery. It is interesting and simple just ten lines of java script code. Use it and enrich your web applications with jquery.

jquery live character count meter

Download Script     Live Demo

Javascript code
$('#contentbox').keyup(function(){} - contentbox is the ID of the textbox. Using $(this).val() getting the textbox value. bar is the div ID of the count meter $('#bar').animate() increasing the width.
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#contentbox").keyup(function()
{
var box=$(this).val();
var main = box.length *100;
var value= (main / 145);
var count= 145 - box.length;

if(box.length <= 145)
{
$('#count').html(count);
$('#bar').animate(
{
"width": value+'%',
}, 1);
}
else
{
alert(' Full ');
}
return false;
});

});
</script>



HTML Code
Contains simple HTML code.
<div>
<div id="count">145</div>
<div id="barbox"><div id="bar"></div></div>
</div>
<textarea id="contentbox"></textarea>

CSS Code
#bar
{
background-color:#5fbbde;
width:0px;
height:16px;
}
#barbox
{
float:right;
height:16px;
background-color:#FFFFFF;
width:100px;
border:solid 2px #000;
margin-right:3px;
-webkit-border-radius:5px;-moz-border-radius:5px;
}
#count
{
float:right; margin-right:8px;
font-family:'Georgia', Times New Roman, Times, serif;
font-size:16px;
font-weight:bold;
color:#666666
}
#contentbox
{
width:450px; height:50px;
border:solid 2px #006699;
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}
Sponsored Links

Share this post

Comments
{ 22 comments }
Motyar said...

Again a nice and useful post.
There should be automatic remove functionality for extra characters.

Hoxxy said...

I use a similar thing for one of my sites, I like the progress bar that I don't have :)

sigit said...

Nice and Very Usefull, Man

Beben said...

my first visit here...
your blog very usefully...
nice to meet you my broder...salom from bandung blogger Indonesia
thanks ^_^'

iRvan said...

there still have a bug. no stop in limit character.

Buzzknow said...

Hi,

this is really simple and usefull.

Thanks :)

encore said...

this is one of the cool site i have ever seen.
as others have said i have also noticed that it doesn't stop after mentioned limit.

the red one said...

good one. thanks

Anonymous said...

cool mate!

Anonymous said...

How can this be integrated to Twitter API?

Twitter like dynamic character counter said...

Thanks. Mine is a Twitter like dynamic character counter.

Anonymous said...

Thanks for the effort. Adding this line:

var trimmed = $(this).val().substring(0,145);
$(this).val(trimmed);


instead of:

alert(' Full ');

will complement the solution.
Regards

prashantvictory said...

This is not working in IE7, please check!

Anonymous said...

It doesn't work on IE 8...

@saikdcom said...

I think you could get better results with this right here:
.bind("input paste", function(e) {
--------------
at least it did for me, I found out about it while doing something like the twitter chars counter and it works perfectly, even if you paste/hold down keys it will keep counting and wont wait until you lift up your fingers to refresh the counter.
Tested on chrome/opera/firefox.

josea said...

can i run this on my localhost only?????do i need internet connection on running these?? pls reply

Srinivas Tamada said...

@Josea

Download jquery.min.js file replace the library file

<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>

to

<script type="text/javascript" src="jquery.min.js"<</script>

Gypaete said...

Thanks

Anonymous said...

Good

Anonymous said...

its having no limit...whats the use then?

Arpit Rawat said...
This comment has been removed by the author.
Arpit Rawat said...

nice. You should also bind "change" event with textbox because its not working when we paste data using mouse.

Post a Comment

Subscribe now!Recent Posts

Categories

Subscribe now!Popular Posts

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Like Me

follow me
products

9lessons labs

9lessons clouds

Android application

Chrome Extension