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

241
Views
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 answers
Answer question

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 Report

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 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!