Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!