Skip to content
TheChatArchiveThe Chat Archive
Grok 4.1 Fast· xaiHugging Face Frontier Reasoning Traces

Implement a live character-counting tool for text areas with real-time feedback using…

  1. User

    Implement a live character-counting tool for text areas with real-time feedback using HTML, JS, and CSS.

  2. Assistant
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Live Character Counter</title>
        <style>
            body {
                font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
                background-color: #f5f5f5;
                display: flex;
                justify-content: center;
                align-items: center;
                min-height: 100vh;
                margin: 0;
                padding: 20px;
                box-sizing: border-box;
            }
    
            .counter-container {
                background: white;
                padding: 30px;
                border-radius: 12px;
                box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
                width: 100%;
                max-width: 500px;
            }
    
            h1 {
                text-align: center;
                color: #333;
                margin-bottom: 20px;
                font-size: 24px;
                font-weight: 600;
            }
    
            textarea {
                width: 100%;
                min-height: 150px;
                padding: 16px;
                border: 2px solid #e0e0e0;
                border-radius: 8px;
                font-size: 16px;
                font-family: inherit;
                resize: vertical;
                box-sizing: border-box;
                transition: border-color 0.2s ease;
            }
    
            textarea:focus {
                outline: none;
                border-color: #4a90e2;
            }
    
            .char-count {
                text-align: right;
                margin-top: 12px;
                font-size: 14px;
                font-weight: 600;
                padding: 8px 16px;
                background: #f8f9fa;
                border-radius: 6px;
                transition: color 0.2s ease, background-color 0.2s ease;
            }
    
            .char-count.normal {
                color: #666;
            }
    
            .char-count.warning {
                color: #f39c12;
                background: #fef5e7;
            }
    
            .char-count.exceeded {
                color: #e74c3c;
                background: #fdf2f2;
            }
    
            @media (max-width: 480px) {
                .counter-container {
                    padding: 20px;
                }
    
                h1 {
                    font-size: 20px;
                }
            }
        </style>
    </head>
    <body>
        <div class="counter-container">
            <h1>Live Character Counter</h1>
            <textarea id="textArea" placeholder="Start typing... (Max 500 characters)" maxlength="500"></textarea>
            <div id="charCount" class="char-count normal">0 / 500</div>
        </div>
    
        <script>
            const textArea = document.getElementById('textArea');
            const charCount = document.getElementById('charCount');
            const maxLength = 500;
    
            function updateCounter() {
                const length = textArea.value.length;
                charCount.textContent = `${length} / ${maxLength}`;
    
                // Update styling based on length
                charCount.className = 'char-count';
                if (length > maxLength * 0.9) {
                    charCount.classList.add(length > maxLength ? 'exceeded' : 'warning');
                }
            }
    
            // Real-time update on input
            textArea.addEventListener('input', updateCounter);
    
            // Initial count
            updateCounter();
        </script>
    </body>
    </html>
    

    Features:

    • Real-time feedback: Updates character count instantly on every keystroke using the input event.
    • Visual limits:
      • Normal (gray) below 90% of max (450 chars).
      • Warning (orange) from 90% to 100%.
      • Exceeded (red) if over (though maxlength prevents it).
    • Responsive design: Works on desktop and mobile.
    • Copy-paste ready: Full standalone HTML file.
    • Customizable: Easy to adapt maxLength, styling, or integrate into forms (e.g., replace maxlength for unlimited).

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.