0

How to stop getting an error for not having a value?

I want my code here to take the input a user puts for the name input and save it for when the user activates the Hello if statement for my Virtual Assistant. Right now the console will throw me an error saying it can't take null as a value. What's going on? If there's any other problems in my code besides that, please point them out. Thank you for your help. https://code.sololearn.com/WTxla4gvmCVM/#js

20th Nov 2018, 10:34 PM
Yosharu
Yosharu - avatar
6 Answers
+ 4
Try to understand the error. It's saying, "can't read property value of null on line 13" means it can't find element with id "userName" and thus returns a null and hence the error. And it's because, the JS part gets loaded before the HTML part and so it can't find the required element. All you need to do is to wrap your entire JS code inside a function which will be called after the HTML window gets loaded. You can do: window.onload= function(){ //Your entire JS code goes here }
21st Nov 2018, 2:04 AM
Шащи Ранжан
Шащи Ранжан - avatar
+ 2
Jack sorry, didn't get you. What do you mean? BTW, your above code is missing a '}' at line 24.
22nd Nov 2018, 5:48 AM
Шащи Ранжан
Шащи Ранжан - avatar
+ 2
Jack no, It should not if you could do it right. Kindly share the link of that broken code.
23rd Nov 2018, 1:32 AM
Шащи Ранжан
Шащи Ранжан - avatar
0
Shashi Ranjan Whole code gets broken
22nd Nov 2018, 4:21 AM
Yosharu
Yosharu - avatar
0
you said to put all of my code inside of a function. that broke my code Shashi Ranjan
22nd Nov 2018, 7:21 PM
Yosharu
Yosharu - avatar