Max Length Counter with Warning
JavaScript
Medium
4 views
Problem Description
You have and . Show how many characters left. If limit exceeded, add class bad on #text.
Output Format
No output (DOM change).
Constraints
Assume #text exists with data-max and #left span exists.
Official Solution
const el=document.getElementById('text');const left=document.getElementById('left');const max=Number(el.dataset.max||0);const upd=()=>{const len=el.value.length;const rem=max-len;left.textContent=String(rem);el.classList.toggle('bad',rem<0);};el.addEventListener('input',upd);upd();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!