I'm starting to study javascript, I implemented this little snippet of code. I spent a long time trying to run the browser until I realized that the problem was calling functions with and without parentheses, note the commented lines.
var buttonElement = document.querySelector('#app button');
var tarefaDigitada = document.querySelector('#app input');
var listElement = document.querySelector('#app ul');
var todos = ['Cofee', 'Beer', 'Water'];
function renderTodos() {
listElement.innerHTML = '';
for (todo of todos) {
var todoElement = document.createElement('li');
var todoText = document.createTextNode(todo);
todoElement.appendChild(todoText);
listElement.appendChild(todoElement);
}
}
function addTodo() {
var text = tarefaDigitada.value;
console.log(tarefaDigitada.value);
todos.push(text);
tarefaDigitada.value = '';
renderTodos();//No parentheses doesn't work
}
renderTodos();
buttonElement.onclick = addTodo;//With parentheses does not work
Even without parameters is necessary to put parentheses? Why on the call to onclick I can not put?