-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordCount.html
36 lines (30 loc) · 877 Bytes
/
wordCount.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!DOCTYPE html>
<html>
<body style="text-align: center">
<h1 style="color: green">Word count for pasted text</h1>
<br>
<h5> <a href="https://www.geeksforgeeks.org/how-to-make-a-word-count-in-textarea-using-javascript/">source, the third example </a> </h5>
<h5> <a href="https://www.geeksforgeeks.org/copyright-information/">Click here for copyright information.</a> <h5>
<textarea id="word" rows="10" cols="60"> </textarea>
<br /><br />
<p>
Word Count:
<span id="show">0</span>
</p>
<script>
document
.querySelector("#word")
.addEventListener("input", function countWord() {
let res = [];
let str = this.value.replace(/[\t\n\r\.\?\!]/gm, " ").split(" ");
str.map((s) => {
let trimStr = s.trim();
if (trimStr.length > 0) {
res.push(trimStr);
}
});
document.querySelector("#show").innerText = res.length;
});
</script>
</body>
</html>