Created
May 24, 2013 16:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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