JavaScript JavaScript and the DOM Making Changes to the DOM Removing Nodes

omar hama
PLUS
omar hama
Courses Plus Student 2,727 Points

Cannot read property 'addEventListener' of null

this is the html code:-

<button class="removeItemButton">Remove last item</button>

and this is the JS code :-

removeItemButton.addEventListener('click', () => { let ul = document.getElementsByTagName('ul')[0]; let li = document.querySelector('li:last-child'); ul.removeChild(li); });

i still gey uncaught typeError: cannot read property 'addEventlistener' of ull

1 Answer

Steven Parker
Steven Parker
203,736 Points

Not shown above is the definition of the variable which is critical to proper operation:

const removeItemButton = document.querySelector('button.removeItemButton');

A typo in this statement could easily create the error you are experiencing.