Skip to content

Instantly share code, notes, and snippets.

@yawboakye
Created May 24, 2013 16:15
Show Gist options
  • Save yawboakye/5644610 to your computer and use it in GitHub Desktop.
Save yawboakye/5644610 to your computer and use it in GitHub Desktop.
Word Counter v0.0.1
<!doctype html>
<html>
<head>
<title>Count 'em</title>
<style>
* { margin:0; padding:0; }
div.wrap { width: 70%; margin: 5% auto; text-align: center; }
textarea { width: 100%; height: 200px; }
p { display: block; background-color: rgb(193, 202, 142);}
</style>
</head>
<body>
<div class="wrap">
<div><textarea class="text"></textarea></div>
<div><p class="display">0 words</p></div>
</div>
<script>
// most important part
var area = document.getElementsByTagName('textarea')[ 0 ],
disp = document.getElementsByTagName('p')[ 0 ];
area.addEventListener( 'keyup', ( function (e) {
var text = this.value.trim();
disp.innerHTML = text.length === 0
? '0 words'
: text.replace(/\s+/g, ' ').split(' ').length + ' words';
}) )
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment