Add "Announcing count" project

This commit is contained in:
Andrea Fazzi 2024-08-05 15:52:57 +02:00
parent 1262186d76
commit 08486cec5e
3 changed files with 43 additions and 0 deletions

View file

@ -0,0 +1,9 @@
# Character count
Display the number of characters a user has typed into a textarea
element in real time as they type.
# Reference
* https://leanwebclub.com/learn/js-essentials/project-character-count/

View file

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
<title>Character count</title>
</head>
<body>
<h1>Character and Word Count</h1>
<label for="text">Enter your text below.</label>
<textarea id="text"></textarea>
<p area-live="polite">You've written <strong><span id="word-count">0</span>
words</strong> and <strong><span id="character-count">0</span>
characters</strong>.</p>
<script src="script.js"></script>
</body>
</html>

View file

@ -0,0 +1,13 @@
let charCounter = document.querySelector("#character-count");
let wordCounter = document.querySelector("#word-count");
let textarea = document.querySelector('#text');
textarea.addEventListener('input', function (event) {
let content = event.target.value;
charCounter.innerText = content.length;
wordCounter.innerText = content.split(/[\s]+/g).filter(function (word) {
return word.length;
}).length;
});