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

112
Views
Invalid event capturing

I just want to check if this is a suitable use-case for using { capture: true } or if there is a different 'best practise' way of doing things.

I have a HTML page which contains a form and multiple dynamic number inputs.

The default message of Please enter a valid value. The two nearest valid values are 4 and 8. is not really suitable, so I would like to customise it.

I first tried something like:

document.addEventListener('invalid', ({ target }) => 
    target.setCustomValidity('Hours must be either 0, 4 or 8.'));

document.addEventListener('input', ({ target }) => 
    target.setCustomValidity(''));

It appears this doesn't work because the invalid event doesn't bubble.

Next was this:

const hoursElements = Array.from(document.querySelectorAll('input[type="number"]'));

hoursElements.forEach((element) =>
    element.addEventListener('invalid', ({ target }) =>
        target.setCustomValidity('Hours must be either 0, 4 or 8.')));

hoursElements.forEach((element) =>
    element.addEventListener('input', ({ target }) =>
        target.setCustomValidity('')));

However, since the page is dynamic, I would need to keep calling the above whenever new inputs were added.

I finally found this which works:

const invalidEvent = ({ target }) => 
    target.setCustomValidity('Hours must be either 0, 4 or 8.');

document.addEventListener('invalid', invalidEvent, { capture: true });

document.addEventListener('input', ({ target }) => 
    target.setCustomValidity(''));
about 4 years ago · Juan Pablo Isaza
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!