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

276
Views
Getting a: Cannot read properties of null error in Javascript when adding an event listener

I'm trying to check whether a user has added at least 8 characters in a password field and add some validation text if they've not but I'm getting this in my console: script.js:13 Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') at script.js:13:17

I believe it relates to this line:

confirmPassword.addEventListener('change', checkPassword);

Full code:

let password = document.querySelector('#password');
let confirmPassword = document.querySelector('#confirm_password');
let passwordValidation = document.createElement('p');
let passwordValidationText = ''

passwordChecker => {
if (password.length <8) {
passwordValidationText === 'Please enter a password, longer than 8 characters';
password.appendChild(passwordValidation);
}
};

confirmPassword.addEventListener('change', passwordChecker);

And the input fields:

<label for="password">Password</label>
        <input type="password" id="password" name="password">
        </div>
        <div class="item">
        <label for="confirm_password">Confirm password</label>
        <input type="password" id="confirm_password" name="confirm_password">

My thinking being I only want to validate when the user has finished typing.

Any idea what I'm doing wrong?

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!