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

213
Views
I am seeing 4th character in output after using substr(0,3) on input characters

I have a problem with an input, I am validating max length with this function, but in my html whenever I enter more than 3 values, it gets added to the input answer output when it should only display max 3 values, I'm not sure why.

And no, I can not use maxlength, because I need to create this dynamically, sometimes my input field has type text, and sometimes it changes to a random number, not always 3.

As you can see on the picture, I typed 333, then input answer was 333, I added another 3 which I do not see in the input field and the answer changed to 3333 which shouldn't happen.

enter image description here

Example:

const input = document.querySelector('input');
const log = document.getElementById('values');

input.addEventListener('input', updateValue);
input.addEventListener('input', cutText);

function cutText(e) {
      e.target.value = e.target.value.substr(0, 3);
      console.log(e.target.value)
    }

function updateValue(e) {
  log.textContent = e.target.value;
}
<input placeholder="Enter some text" name="name"/>
<p id="values"></p>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This is because of the ordering of the event, You are first updating the value of the log element and then setting the value of the input.

If you have input aaaa then it will first set the value of the log element to aaaa and then set the input as aaa

If you would place curText first and then updateValue then It will behave as expected

input.addEventListener('input', cutText);
input.addEventListener('input', updateValue);

const input = document.querySelector('input');
const log = document.getElementById('values');

input.addEventListener('input', cutText);
input.addEventListener('input', updateValue);

function cutText(e) {
  e.target.value = e.target.value.substr(0, 3);
  console.log(e.target.value)
}

function updateValue(e) {
  log.textContent = e.target.value;
}
<input placeholder="Enter some text" name="name" />
<p id="values"></p>

1) Alternatively you can add a single listener and call function in it.

input.addEventListener( 'input', (e) => {
    cutText(e);
    updateValue(e);
} )

2 You can just do it in a single callback function

input.addEventListener( 'input', ( e ) => {
    const value = e.target.value.substr( 0, 3 );
    e.target.value = value;
    log.textContent = value;
} )
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!