This simple JavaScript code doesn’t work for some reason. Please help 

I get this error this code line:

Code:

 

 

 


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:

Code:

 


<body onclick="timeAndGreeting('timeTxt', 'greetingTxt')">

 

the relevant p tags:

Code:

 


<p id="timeTxt"> iii</p> <p id="greetingTxt"> jjj</p>

 

Java Script:

Code:

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;
}

 

DanS Default Asked on February 24, 2018 in Computer.
Add Comment
  • 2 Answer(s)

    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!

    tinyfeet79 Default Answered on November 8, 2018.
    Add Comment

    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?

    Karolina90 Default Answered on February 11, 2019.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.