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

243
Visualizações
How can I solve Uncaught TypeError?

While trying to push element in array, I receive an error:

main.js:1 Uncaught TypeError: Cannot read properties of null (reading 'document') at main.js:1:50

Here is my code:

<!DOCTYPE html>
<html>

<body>
  <button id="input">Click Here</button>

  <script>
    const newItem = document.getElementById('.input').document.addEventListener('click', function(addNewItem) {
      let move = ["L", "L", "R", "F", "N"];
      const newMove = move.push('newItem');
      console.log(newMove);
    })
  </script>

</body>

</html>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The error is being caused by document.getElementById('.input'). which returns null. There is not .input element, you need to use the correct spelling, like this: 'input'

about 4 years ago · Juan Pablo Isaza Relatório

0

There are two mistakes in your code.
  1. You are targeting the id by using document.getElementById(".input"), however; id in html is input not .input. You have to remove . before input.
  • Remove document before document.addEventListener
    document.getElementById('input').addEventListener('click', function (arg) 
    {
    
    });
    


I have included a modified example of your code below with the appropriate corrections.

<!DOCTYPE html>
<html>
<body>
    <button id="input">Click Here</button>

    <script>
        const newItem = document.getElementById('input').addEventListener('click', function (addNewItem) {
            let move = ["L", "L", "R", "F", "N"];
            const newMove = move.push('newItem');
            console.log(newMove);
        });
    </script>

</body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório
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