Add "Announcing count" project
This commit is contained in:
parent
1262186d76
commit
08486cec5e
3 changed files with 43 additions and 0 deletions
9
js-essentials/announcing-count/README.md
Normal file
9
js-essentials/announcing-count/README.md
Normal 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/
|
||||||
|
|
21
js-essentials/announcing-count/index.html
Normal file
21
js-essentials/announcing-count/index.html
Normal 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>
|
13
js-essentials/announcing-count/script.js
Normal file
13
js-essentials/announcing-count/script.js
Normal 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;
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue