0

When I run my page on liveserver, sometimes everything works fine and othertimes I get Cannot read property 'classList' of null. Why does it happen sometimes and other time not?

1 Answers1

0

In this case, it seems that your element is null. That means the element you are trying to get the classlist is not avaliable in your context.

This can happens if this element is loaded by an ajax call, for example. Sometimes the ajax call takes more time, and when you search for the element, it's not there yet.

If this is the case, you can do some tricks (like setTimeout) and wait for the element to be avaliable or even using mutations.

Check here Detect changes in the DOM

danielarend
  • 1,379
  • 13
  • 26