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

287
Vistas
How to replace the previous user input with new input when max length is reached?

I have an input which max length = 3

#username {
  width: 30px;
}
  <input type="text" id="username" name="username" maxlength="3">

Is there a way to automatically replace the oldest user input with the latest user input when max length is reached?

E.g. user types 'a', 'b', 'c', 'abc' is showing in the input. If user types 'd', 'bcd' should be shown in the input.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can get the last 3 characters of the input using String.prototype.substr(), then replace the input with the last 3 characters when someone inputs a value.

document.getElementById('username').addEventListener('input', function() {
  this.value = this.value.substr(-3);
});
<input type="text" id="username" name="username">

You will have to remove the maxlength="3", because otherwise people don't have the chance to input the 4th character used to rotate the string.

about 4 years ago · Juan Pablo Isaza 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