Skip to content

Instantly share code, notes, and snippets.

@devluis
Created December 6, 2013 05:35
Show Gist options
  • Save devluis/7819068 to your computer and use it in GitHub Desktop.
Save devluis/7819068 to your computer and use it in GitHub Desktop.
Live Word and Character Counter using jQuery
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>Live Word and Character Counter using jQuery</title>
<style>
textarea{
width:300px;
height:300px;
}
</style>
<script>
function count(){
var txtVal = $('textarea').val();
var words = txtVal.trim().replace(/\s+/gi, ' ').split(' ').length;
var chars = txtVal.length;
if(chars===0){words=0;}
$('#counter').html('<br>'+words+' words and '+ chars +' characters');
}
count();
$('textarea').on('keyup propertychange paste', function(){
count();
});
</script>
</head>
<body>
<textarea></textarea>
<div id="counter"></div>
</body>
</html>
@robriley78
Copy link

Thank you very much. I have borrowed and adapted. Thanks for sharing.

@mianaliasjad
Copy link

thanks <3

@VitorCaique78
Copy link

Thanks for helping, friend!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment