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

214
Vistas
Get Javascript maxLength in input

I have a problem with downloading and displaying the value, when set to "rigidly" maxLenght - everything works fine, but when I want the script to download the value myself, I have a problem.

The script is supposed to get the value maxlenght = "" from each <input> and after typing by the user it will print out how many characters are left.



var maxLen = document.getElementsByClassName("handlerWorld").maxLength;

function countChar(jqObj) {
    var len = jqObj.val().length;
    var diff = maxLen - len;

    if (len > maxLen) {
        len = maxLen;
        diff = 0;
    }
    jqObj.val(jqObj.val().substr(0, len)).prev('label').find('span.chars-twenty').text(diff);
}

$(document).ready(function () {
    $("[class*='handlerWorld']").keyup(function () {
        countChar($(this));
    }).each(function () {
        countChar($(this));
    });
});

My HTML:

<div class="form-group">
          <label for="nameIput">Nazwa firmy <span>Remaining: <span class="chars-twenty"></span></label>
          <input type="text" id="name" maxlength="140" name="name" class="form-control handlerWorld">
        </div>
        <div class="form-group">
          <label for="urlInput">URL <span>Remaining: <span class="chars-twenty"></span></label>
          <input type="text" id="url" name="url" maxlength="100" class="form-control handlerWorld">
          
        </div>

Edit:

Now I would like it to show the number of characters, e.g .:

10/140

quantity written / quantity in the value "maxlenght"

$("#add-category").find('span.maxchar').text(maxLen);

They work but not as they should.

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

0

Move the MaxLength calculation to the Count method

var maxLen = jqObj.attr("maxlength");

here's fiddle for you: https://jsfiddle.net/d53yjpr8/1/

function countChar(jqObj) {
  var maxLen = jqObj.attr("maxlength");
  var len = jqObj.val().length;
  var diff = maxLen - len;

  if (len > maxLen) {
    len = maxLen;
    diff = 0;
  }
  jqObj.val(jqObj.val().substr(0, len)).prev('label').find('span.chars-twenty').text(diff);
}

$(document).ready(function() {
  $("[class*='handlerWorld']").keyup(function() {
    countChar($(this));
  }).each(function() {
    countChar($(this));
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="form-group">
  <label for="nameIput">Test <span>Remaining:</span> <span class="chars-twenty"></span></label>
  <input type="text" id="name" maxlength="140" name="name" class="form-control handlerWorld">
</div>
<div class="form-group">
  <label for="urlInput">URL <span>Remaining:</span> <span class="chars-twenty"></span></label>
  <input type="text" id="url" name="url" maxlength="100" class="form-control handlerWorld">

</div>

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