Skip to content

Instantly share code, notes, and snippets.

@elimence
Created May 24, 2013 16:28
Show Gist options
  • Save elimence/5644710 to your computer and use it in GitHub Desktop.
Save elimence/5644710 to your computer and use it in GitHub Desktop.
A too simplified and non-beautified way of counting words entered into a text area
<html>
<head>
<title>WORD COUNT TEST</title>
<style type="text/css">
#text {
height: 100px;
width: 200px;
}
</style>
</head>
<body>
<textarea id='text'></textarea><br>
<input id='info'>
</body>
<script>
var info = document.getElementById('info');
var area = document.getElementById('text');
area.onkeypress = count;
var prevChar = ''
var wordCount = 0
function count(evt) {
key = evt.keyCode;
var keyS = isDel(key);
var preS = isDel(prevChar);
if (preS || (prevChar == '' && !keyS)) {
if (!(preS && keyS))
wordCount++;
info.value = wordCount;
}
prevChar = key;
}
function isDel(key) {
if (key == 13 || key == 32)
return true;
else
return false;
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment