S
S
Spooky 20202022-02-07 22:30:18
Frontend
Spooky 2020, 2022-02-07 22:30:18

How to handle "object may be null" when getting elements via querySelector/getElementById/etc?

Of course you can write something like: document.querySelector(`elmentname`) ? .classList.add("disabled")
But I would like to get an error if the element is not found, if for example I specified a wrong selector.
What would the compiler not swear and throw an exception when calling .classList.add("disabled") ?
How to do it right?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question