-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter_counter.js
33 lines (28 loc) · 1.37 KB
/
character_counter.js
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
function update_character_counter(field) {
const maxlength = field.getAttribute('maxlength').trim();
if (maxlength === '') return;
const remaining = maxlength - field.value.length;
let counter_color = '#444444';
if (remaining < Math.min(0.25 * maxlength, 40)) {counter_color = '#c87800';}
if (remaining < Math.min(0.1 * maxlength, 10)) {counter_color = '#ff0040';}
if (field.nextElementSibling === null || !field.nextElementSibling.classList.contains('character-counter')) {
field.parentNode.insertBefore(document.createElement('p'), field.nextSibling);
field.nextElementSibling.classList.add('character-counter');
field.nextElementSibling.style.textAlign = 'right';
field.nextElementSibling.style.fontStyle = 'italic';
field.nextElementSibling.style.fontSize = '0.85em';
}
field.nextElementSibling.innerText = remaining + ' characters remaining.';
field.nextElementSibling.style.color = counter_color;
}
document.addEventListener('input', function(event) {
if (event.target.classList.contains('count') && event.target.hasAttribute('maxlength')) {
update_character_counter(event.target);
}
});
document.addEventListener('blur', function(event) {
const sibling_character_counter = event.target.parentNode.querySelector('.character-counter');
if (sibling_character_counter !== null) {
sibling_character_counter.parentNode.removeChild(sibling_character_counter);
}
}, true);