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>
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'
id by using document.getElementById(".input"), however; id in html is input not .input. You have to remove . before input.document before document.addEventListener
document.getElementById('input').addEventListener('click', function (arg)
{
});
<!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>