Last active
October 19, 2022 20:59
-
-
Save angusgrant/94a025224f1023218bfb97571d09ec9d to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Character & Word 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>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 elements | |
let text = document.querySelector('#text'); | |
let charCount = document.querySelector('#character-count'); | |
let wordCount = document.querySelector('#word-count'); | |
// Listen for changes to the text area content | |
text.addEventListener('input', function () { | |
//split by space and then filter the array length | |
let arrayWord = text.value.split(' '); | |
let arrayWordFiltered = arrayWord.filter(function (item) { | |
return item.length > 0 | |
}) | |
wordCount.textContent = arrayWordFiltered.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