-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvanilla-9.html
39 lines (31 loc) · 1.18 KB
/
vanilla-9.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
37
38
39
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Vanilla js project 2 alt</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body class="container">
<form class="pt-2">
<div class="form-group">
<label for="text">Enter your text below.</label>
<textarea id="text" class="form-control"></textarea>
</div>
</form>
<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>
var area = document.querySelector('#text');
var counter = document.querySelector('#character-count');
var wordCounter = document.querySelector('#word-count');
//console.log(counter);
area.addEventListener('input', function(){
// textLength is a textarea specific property
counter.textContent = area.textLength;
// regex that takes into account spaces, newlines, breaks etc...
wordCounter.textContent = area.value.split(/[\n\r\s]+/g).filter(function(index) {
return index.length > 0;
}).length;
});
</script>
</body>
</html>