This simple JavaScript code doesn’t work for some reason. Please help
I get this error this code line:
document.getElementById(idTime1).innerHTML = 'sfs';
Error: Unhandled exception at line 3, column 5 in http://localhost:58608/js1.js
0x800a138f – JavaScript runtime error: Unable to set property ‘innerHTML’ of undefined or null reference
Html (relevant codes):
the body onload:
<body onclick="timeAndGreeting('timeTxt', 'greetingTxt')">
the relevant p tags:
<p id="timeTxt"> iii</p> <p id="greetingTxt"> jjj</p>
Java Script:
function timeAndGreeting(idTime1, idGreeting1) { document.getElementById(idTime1).innerHTML = 'sfs'; var today = new Date(); var h = today.getHours(); var m = today.getMinutes(); var s = today .getSeconds(); m = checkTime(m); s = checkTime(s); document.getElementById(idTime1).innerHTML = h + ":" + m + ":" + s; var t = setTimeout(timeAndGreeting, 500); // Greetings if (h < 12) { document.getElementById(idGreeting1).innerHTML = "<b>Good morning!</b>"; } if (h > 12) { document.getElementById(idGreeting1).innerHTML = "<b>Good afternoon!</b>"; } if (h == 12) { document.getElementById(idGreeting1).innerHTML = "<b>Good Noon!</b>"; } } function checkTime(i) { if (i < 10) { i = "0" + i }; // add zero in front of numbers < 10 return i; }
It tells you that it is an undefined reference because getElementById is expecting you to pass a string. It thinks that idTime1 is a variable, when in fact it should be a string. So, you should put quotation marks around it :
document.getElementById("idTime1").innerHTML = 'sfs';
Do the same thing every time you use getElementById. Don’t forget the quotation marks!
Hi! I need your help. I recently created my own website Mr Bet casino review. I want you to appreciate it. It’s important. I want to know about all the pros and cons of my site. I want to make the site even better. You help me?