TypeError: Cannot read property 'classList' of null
In my case I forgot to add a "." on my querySelector
to select a class. If you are trying to select a class or an ID, check that you have the appropriate punctuation. For example:
const someclass = document.querySelector('.someclass');
To select an ID:
const someid = document.querySelector('#someid');
Also make sure that you spell the attribute that you trying to select correctly. Let's say for example you would like to select input
but you misspell it on the selector below, it wont work.
Wrong one:
const input = document.querySelector('inputo'); 'inputo doesn't exist.
Correct one:
const input = document.querySelector('input');
It means that document.getElementById("lastName")
is returning undefined
and you're trying to call classList
on undefined
itself.
In your HTML input
has the attribute name
which equals lastName
but there is no actual id="lastname"
Either add the attribute id
to your input
or change getElementById
to getElementsByName
.
Note that getElementsByName
doesn't return a single item.