-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
42 lines (41 loc) · 1.04 KB
/
script.js
File metadata and controls
42 lines (41 loc) · 1.04 KB
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
40
41
42
const charVal = document.getElementById("textarea");
const totalChar = document.getElementById("total-counter");
const remainChar = document.getElementById("remain-counter");
let userChar = 0;
const charCount = (elem) => {
totalCount = 0;
for (let i = 0; i < elem.length; i++) {
if (elem[i] === " ") {
continue;
} else {
totalCount++;
}
}
return totalCount;
};
const limitReach = (val) => {
if (val > 150) {
alert("Max Limit Reached !");
charVal.setAttribute("maxlength", charVal.value.length);
return true;
} else {
charVal.setAttribute("maxlength", " ");
return false;
}
};
const updateCounter = () => {
char = charCount(charVal.value);
if (limitReach(char)) {
totalChar.innerText = 150;
remainChar.innerText = 0;
} else {
totalChar.innerText = char;
remainChar.innerText = 150 - char;
}
};
const copyText = () => {
charVal.select();
charVal.setSelectionRange(0, 9999);
navigator.clipboard.writeText(charVal.value);
};
charVal.addEventListener("keyup", () => updateCounter());