Some Guu
Some Guu

Reputation: 35

Uncaught TypeError: Cannot read property 'addEventListener' of undefined at app.js:4

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

Answers (1)

Muhammad Usman
Muhammad Usman

Reputation: 10148

document.btn.addEventListener('click',onClick); should be

btn.addEventListener('click',onClick);

Upvotes: 2

Related Questions