Reputation: 123
When I run the code below, Safari's debug console tells me:
TypeError: Result of expression 'document.getElementById("txtHint")' [null] is not an object.
It seems to be throwing the error at this line:
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
function showItem(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200 && xmlhttp.responseText!='')
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getitem.php?q="+str,true);
xmlhttp.send();
}
I am unsure of why this eror is thrown. There is absolutely a DIV with an ID of "txtHint" and yet safari cannot seem to run this code correctly. I guess my question is, what is wrong with this block of code.
Upvotes: 0
Views: 586
Reputation: 186662
Is this not a duplicate question? Anyway, try throwing the script before the end body tag or invoking the function after dom ready / window load.
Upvotes: 1
Reputation: 522382
It's not a syntax issue, it tells you that document.getElementById("txtHint")
returned null
(i.e. there was no element with that id), which means the result (null
) is not an object, which means you can't call .innerHTML
on it.
Upvotes: 1
Reputation: 327
Yes, agree with Ian Henry. From the error message, I guess that there is no element whose id is "txtHint" in the document .
Upvotes: 0