Skip to content
Advertisement

Uncaught TypeError: Cannot read property ‘value’ of null

I’m getting error in this code, I’m trying to do an event where in when the page is load, it will do the event. But the problem is when I go to other function, but same page, it gets a error of null on that variable. It has no problem when I execute this codes, but when I’m on other part of my codes this error occurs.

Uncaught TypeError: Cannot read property ‘value’ of null

JavaScript

Advertisement

Answer

I am unsure which of them is wrong because you did not provide your HTML, but one of these does not exist:

JavaScript

There is either no element with the id cal_preview, year, holiday, cal_option, or some combination.

Therefore, JavaScript is unable to read the value of something that does not exist.

EDIT:

If you want to check that the element exists first, you could use an if statement for each:

JavaScript

You could obviously change the else statement if you want or have no else statement at all, but that is all about preference.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement