Count Characters in Textarea Without Spaces
Character count tutorial by EasyProgramming.lnet
by Nazmus Nasir
HTML
<!-- Easy JavaScript # 32 - Character Count -->
<!-- Supplemental code based on comment in https://www.youtube.com/watch?v=pGdNiYzth44 -->
<p>
Welcome to the 32nd Easy JavaScript tutorial, part of <a href="http://www.easyprogramming.net">EasyProgramming.net</a>. Let's create a script that keeps track of the number of characters we input in a textarea!
</p>
<p>
What you'll need to know: Array Methods, Event Listeners, input/output (and other basics)
</p>
<p>
Let's take a look!
</p>
<p>
<textarea id="myText" height="500"></textarea>
</p>
<p>
<span id="wordCount">0</span> Characters
</p>
CSS
textarea {
width: 500px;
height: 150px;
}
JavaScript
var myText = document.getElementById("myText");
var wordCount = document.getElementById("wordCount");
myText.addEventListener("keyup",function(){
var characters = myText.value.split('');
wordCount.innerText = characters.filter( item => {
return (item != ' ');
}).length;
});