Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

278
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda