Reputation: 35
Sorry if this is simple but I'm new and I don't really get what is this about. I'm trying to add li's to ul when a tag called btn is pressed and I get this error.
const btn = document.querySelector('.btn');
const list = document.querySelector('.list');
document.btn.addEventListener('click',onClick);
function onClick(e){
console.log('clicked');
const li = document.createElement('li');
li.appendChild(document.createTextNode('New item'));
list.appendChild(li);
e.preventDefault();
}
Upvotes: 0
Views: 65
Reputation: 10148
document.btn.addEventListener('click',onClick);
should be
btn.addEventListener('click',onClick);
Upvotes: 2