Skip to content

Instantly share code, notes, and snippets.

@angusgrant
Created October 21, 2022 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angusgrant/aa603ebc44d707b92819da814e159c35 to your computer and use it in GitHub Desktop.
Save angusgrant/aa603ebc44d707b92819da814e159c35 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Character & Word Count - Announcing the Count</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
body {
margin: 0 auto;
max-width: 40em;
width: 88%;
}
label {
display: block;
width: 100%;
}
textarea {
min-height: 24em;
width: 100%;
}
</style>
</head>
<body>
<h1>Character & Word Count</h1>
<label for="text">Enter your text below.</label>
<textarea id="text"></textarea>
<p aria-live="polite" aria-atomic="true">You've written <strong><span id="word-count">0</span> words</strong> and <strong><span id="character-count">0</span> characters</strong>.</p>
<script>
// Get the text area, and word and character count elements
let text = document.querySelector('#text');
let wordCount = document.querySelector('#word-count');
let charCount = document.querySelector('#character-count');
// Listen for changes to the text area content
text.addEventListener('input', function () {
// Get the word count
let words = text.value.split(/[\s]+/g).filter(function (word) {
return word.length;
});
// Display the word count
wordCount.textContent = words.length;
// Display the characters count
charCount.textContent = text.value.length;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment