import '../../../../scss/components/form/_count_chars.scss';

document.querySelectorAll('.count-chars').forEach(element => {
    countChars(element);
    element.addEventListener('keyup', function () {
        countChars(element);
    })
})

function countChars(element) {
    const span = document.createElement('span');
    span.classList.add('count-chars-counter');
    const charCount = element.value.length;
    span.innerText = `${charCount} caractère${charCount > 1 ? 's' : ''}`;

    element.parentNode.querySelector('.count-chars-counter')?.remove();
    element.parentNode.appendChild(span);
}
