Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  33] [ 1]  / answers: 1 / hits: 17969  / 11 Years ago, fri, september 13, 2013, 12:00:00

I'm trying to write a pretty simple script to set an element's innerHTML to the time. However, Javascript keeps throwing a Cannot set property 'innerHTML' of undefined error. During debugging, I've simplified my script to the point that it runs right after element (a <span>) is coded, so I know it should have loaded already. I've also tried running this script as a <body onload= argument - same error. I can't tell what I'm doing wrong.



<div style=position: fixed; right: 10px; top: 10px; width: auto; font-size: 16pt; border: 2px solid #000000;>
<span id=clock>Loading...</span>
<script type=application/x-javascript>
function setClock(spanid){
var d = new Date();
document.getElementById[spanid].innerHTML = d.getHours() + : + d.getMinutes() + : + d.getSeconds;
}
setClock(clock);
</script>
</div>


Any help is appreciated!


More From » html

 Answers
98

You are not calling getElementById, you are attempting to index it. Since it is not an array and does not expose array-like behavior, the result is undefined. Replace your getElementById[spanId] with getElementById(spanId).


[#75718] Thursday, September 12, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
demondp

Total Points: 154
Total Questions: 97
Total Answers: 99

Location: Mali
Member since Thu, Jul 9, 2020
4 Years ago
;