Uncaught reference error | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Uncaught reference error

Hi Iā€™m working with VS Studio and Iā€™ve been trying to execute some code however I keep getting uncaught reference error saying ā€œgreeting is undefinedā€. Iā€™m using chrome and the error appears on the console log. Here is the code Iā€™m trying to execute. <button type=ā€œbuttonā€ onclick=ā€œtestFunc()ā€> <p Iā€™d=ā€œdemoā€></p> Function testFunc() { var hour = new Date().getHours(); var greeting; If (hour < 18) greeting = ā€œGood dayā€; } else { greeting = ā€œGood evening ā€œ } document.getElementbyId(ā€œdemoā€).innerHTML = greeting; Iā€™ve tried putting parentheses after the var greeting () but still doesnā€™t work. How can I solve this.

3rd Jul 2020, 3:14 PM
Yamkela Mazwi
Yamkela Mazwi - avatar
6 Respostas
+ 8
You getting this error because your javascript part id not working may be try to fix it.
4th Jul 2020, 4:28 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 4
Please show your code using code playground https://www.sololearn.com/post/75089/?ref=app
3rd Jul 2020, 3:28 PM
Gordon
Gordon - avatar
+ 2
The <button> tag didn't have its counter </button> pair. 'id' attribute of the <p> tag is messed up by autocorrect feature. You used slanted double quotes almost everywhere. Change them to standard double quotes. Javascript is a case sensitive language, so please mind letter cases, they do matter. Function -> function getElementbyId -> getElementById
3rd Jul 2020, 3:48 PM
Ipang
+ 1
<p Iā€˜d=ā€ždemoā€œ> shall be <p id=ā€ždemoā€œ>
3rd Jul 2020, 3:18 PM
JaScript
JaScript - avatar
+ 1
really appreciate it man thanks
3rd Jul 2020, 3:52 PM
Yamkela Mazwi
Yamkela Mazwi - avatar
0
youā€™re also missing the opening { of your if and the closing } of your function.
4th Jul 2020, 5:28 PM
Stefan