-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
28 lines (21 loc) · 1 KB
/
app.js
File metadata and controls
28 lines (21 loc) · 1 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
const productNameInputElement = document.getElementById("product-name");
const remainingCharsElement = document.getElementById("remaining-chars");
// console.dir(productNameInputElement);
const maxAllowedChars = productNameInputElement.maxLength;
function updateRemainingCharacters(event) {
const enteredText = event.target.value;
const enteredTextLength = enteredText.length;
const remainingCharacters = maxAllowedChars - enteredTextLength;
remainingCharsElement.textContent = remainingCharacters;
if (remainingCharacters === 0) {
productNameInputElement.classList.add("error");
remainingCharsElement.classList.add("error");
} else if (remainingCharacters <= 10) {
productNameInputElement.classList.add("warning");
remainingCharsElement.classList.add("warning");
} else {
productNameInputElement.classList.remove("error", "warning");
remainingCharsElement.classList.remove("error", "warning");
}
}
productNameInputElement.addEventListener("input", updateRemainingCharacters);