I'm trying to pass the array elements with for (of) to the html elements I created by js. The problem is that when I try to put the created in the js inside, using appendChild, the error code. Home This is the javascript:
var listElement = document.querySelector('#app ul');
var inputElement = document.querySelector('#app input');
var buttonElement = document.querySelector('#app button');
var todos = [
'Fazer café',
'Estudar Javascript',
'Lavar a Louça'
];
function renderTodos(){
for(todo of todos){
var todoElement = document.createElement('li');
var todoText = document.createTextNode(todo);
todoElement.appendChild(todoText);
listElement.appendChild(todoElement);
}
}
renderTodos();
This is the html;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content Language" content="pt-br">
<title>Parte 3</title>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div id="app">
<ul>
</ul>
<input type="text" placeholder="Digite um To do">
<button>Adicionar</button>
</div>
</body>
</html>
Error occurred:
script.js: 18 Uncaught TypeError: Can not read property 'appendChild' of null at renderAll (script.js: 18) at script.js: 22