Skip to content
Advertisement

Why is there an error with onclick function?

I’m working on an assignment that asks us to use Promise, and when I run the script, I am getting this error on line 37:

Uncaught TypeError: Cannot set property ‘onclick’ of null

I can’t understand why the onclick is throwing this error, because I have used buttons before with similar functionality, and never had an issue. Code is below:

JavaScript

Advertisement

Answer

JavaScript

Null was returned because you used getElementById and there are no ID assigned to the button. Try:

JavaScript

Note:

Hard-assigning id to the button may not be ideal if there are multiple submit buttons. Potentially look for a more descriptive name and find a query-selector that suits your needs.

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