Build a Real-Time Character Counter with Vanilla JavaScript and ARIA Live Regions

A character counter with an aria-live region

A field with a length limit should tell the user how much room is left while they type, not after the form was sent back with an error. A visible counter is easy; making it useful for screen reader users needs an aria-live region and some restraint, because announcing every keystroke makes typing unbearable.

Problem: A review form limits the text to 500 characters, users only learn it when the server rejects the review, and screen reader users get no feedback at all.

Solution: Add a counter element after each field with data-max-length, connect it with aria-describedby, update the visible text on every input event, and update a separate aria-live="polite" region only at a few thresholds. Keep the limit on the server as well.

The markup

<label for="review">Your review</label>
<textarea id="review" name="review" rows="6" data-max-length="500"></textarea>

The native maxlength attribute would stop typing at the limit, but it cuts off pasted text silently and gives no feedback; a counter lets the user see they are over and shorten the text themselves.

The script

document.querySelectorAll( '[data-max-length]' ).forEach( ( field ) => {
    const max = Number( field.dataset.maxLength );
    const counter = document.createElement( 'p' );
    const live = document.createElement( 'span' );
    counter.id = field.id + '-counter';
    counter.className = 'ha-counter';
    live.className = 'screen-reader-text';
    live.setAttribute( 'aria-live', 'polite' );
    field.after( counter, live );
    field.setAttribute( 'aria-describedby', ( ( field.getAttribute( 'aria-describedby' ) || '' ) + ' ' + counter.id ).trim() );

    let lastStep = null;
    const update = () => {
        const left = max - Array.from( field.value ).length;
        counter.textContent = left >= 0 ? `${ left } characters left` : `${ -left } characters too many`;
        counter.classList.toggle( 'is-over', left < 0 );

        // announce only when crossing 50, 20, 0 left
        const step = left < 0 ? 'over' : [ 50, 20, 0 ].find( ( s ) => left <= s );
        if ( step !== undefined && step !== lastStep ) {
            live.textContent = counter.textContent;
        }
        lastStep = step;
    };

    field.addEventListener( 'input', update );
    update();
} );

Array.from( value ).length counts characters as the user sees them; value.length counts UTF-16 units, so an emoji would count as two. The visible counter is linked with aria-describedby, so a screen reader reads the remaining count when the field gets focus. The live region speaks only when the user reaches 50, 20 and 0 characters left, and once when the text gets too long.

screen-reader-text is the WordPress class that hides text visually but keeps it for screen readers; themes that support accessibility already have it.

Style

.ha-counter {
    margin: 0.25rem 0 0;
    font-size: 0.875rem;
    color: #555;
}

.ha-counter.is-over {
    color: #b00020;
    font-weight: 600;
}

The server check

$review = sanitize_textarea_field( wp_unslash( $_POST['review'] ?? '' ) );
if ( mb_strlen( $review ) > 500 ) {
    $errors->add( 'review_length', __( 'The review is limited to 500 characters.', 'textdomain' ) );
}

NOTE: The counter is a help, not a protection: the browser’s script can be switched off or bypassed, so the limit must also be checked in PHP with mb_strlen(), as above. For other input checks see sanitizing and validating user input.