🍦 Vanilla JavaScript

Projects for the Vanilla JS Academy

Day 9

Character and Word Count

Goal

Modify the previous script to also display a live word count.

Project

You’ve written 0 words and 0 characters.

View Source
(function() {
	let elText = document.querySelector( "#text" );
	let elCharacterCount = document.querySelector( "#character-count" );
	let elWordCount = document.querySelector( "#word-count" );
	elText.addEventListener( "input", e =>
	{
		elCharacterCount.innerText = e.target.value.length;
		elWordCount.innerText = e.target.value.trim()
			.split( /\s+/g )
			.length;
	});
})();