Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
Character countdown isn't working when value is present

I have an input field and I added a character countdown to it via vanilla Javascript. The user can add a maximum of 60 characters and the counter shows how many characters remaining while he is typing.

When the input field is empty, it shows that 60 characters are remaining. However when there is a value in the field already (when you are not typing), it shows that 60 characters are remaining. So it's not counting the number of characters of the value. It only takes it of from the 60 when I click in the input field and start typing.

If there is a value in the field, how can I make sure its characters are deducted from the max character length automatically?

Here is my code:

const POST_TITLE = document.getElementById("post_title");
const MAX_LENGTH = 60;
const CHARS = document.getElementById("chars");

function countCharacters() {
    let enteredChars = POST_TITLE.value.length;
    let charsLeft = MAX_LENGTH - enteredChars;

    if(charsLeft > 35) {
        CHARS.textContent = charsLeft + " characters remaining. Please add more characters for better SEO.";
        CHARS.style.color = "red";
    } else if(charsLeft > 10) {
        CHARS.textContent = charsLeft + " characters remaining. Almost there! Just add a few more characters to have a great SEO title.";
        CHARS.style.color = "orange";
    } else {
        CHARS.textContent = charsLeft + " characters remaining. Awesome! Between 50-60 characters is a great SEO title.";
        CHARS.style.color = "green";
    }

    if(POST_TITLE.value.length == '') {
        CHARS.style.color = "black";
        CHARS.textContent = charsLeft + " characters remaining. Please add a meta title.";
    }
}

POST_TITLE.addEventListener("keyup", countCharacters);
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  
  <form action="#" class="mt-4">
    <div class="mb-3">
      <label for="post_title" class="form-label">Post title</label>
      <input type="text" class="form-control" id="post_title" minlength="5" maxlength="60" value="some value">
      <span id="chars">60 characters remaining. Please add a meta title.</span>
    </div>
  </form>
</div>

So here I already added a value to the input field so you can see that when the page loads it's not counting the characters of the value. I've tried different events instead of the keyup, like input but didn't work.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

As mentioned in the comment section, simply calling the countCharacters(); function under it's scope, will trigger the event that you seek for.

Note that the white spaces here do count as the characters.

const POST_TITLE = document.getElementById("post_title");
const MAX_LENGTH = 60;
const CHARS = document.getElementById("chars");

function countCharacters() {
    let enteredChars = POST_TITLE.value.length;
    let charsLeft = MAX_LENGTH - enteredChars;

    if(charsLeft > 35) {
        CHARS.textContent = charsLeft + " characters remaining. Please add more characters for better SEO.";
        CHARS.style.color = "red";
    } else if(charsLeft > 10) {
        CHARS.textContent = charsLeft + " characters remaining. Almost there! Just add a few more characters to have a great SEO title.";
        CHARS.style.color = "orange";
    } else {
        CHARS.textContent = charsLeft + " characters remaining. Awesome! Between 50-60 characters is a great SEO title.";
        CHARS.style.color = "green";
    }

    if(POST_TITLE.value.length == '') {
        CHARS.style.color = "black";
        CHARS.textContent = charsLeft + " characters remaining. Please add a meta title.";
    }
}
countCharacters();
POST_TITLE.addEventListener("keyup", countCharacters);
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  
  <form action="#" class="mt-4">
    <div class="mb-3">
      <label for="post_title" class="form-label">Post title</label>
      <input type="text" class="form-control" id="post_title" minlength="5" maxlength="60" value="some value">
      <span id="chars">60 characters remaining. Please add a meta title.</span>
    </div>
  </form>
</div>

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda