I'm developing a web application using javascript / ajax .
The problem is that in my application (a kind of social network) I need to read information about users (posts in a database). To do this I use a PHP dedicated service with the interactions with database. The service returns HTML formatted for the main page.
Now, I use this service with an AJAX call to bring the HTML and injects it into my page. The problem is that when I try to get an element from the injected HTML with javascript:
document.getElementById('commentsArea_xxx').innerHTML=xmlhttp.responseText;
getElementById
returns null
.
I think the problem is that after the AJAX call the browser does not update the document and so 'commentsArea_xxx' doesn't exist in the document!
How do I fix this problem?
This is my code:
function moreComments(id){
var footerIcon = document.getElementById('footer');
footerIcon.innerHTML="<div style='text-align:center;'><img src='images/home_selected.png' onClick='loadHome()'/><img id='cameraButton' src='images/camera.png'/><img src='images/list.png' onClick='loadMyVideo()'/></div>";
pageOld=pageCurrent;
pageCurrent=0;
document.getElementById("ajaxContent").innerHTML="<div id='wrapper' style='top:95px;'><div id='scroller'><ul id='thelist'><li style='text-align:center;'><img src='images/loading.gif' style='text-align:center;'></li></ul></div></div>";
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("commentsArea_"+id).innerHTML=xmlhttp.responseText;
loaded();
me();
setTimeout(function () {myScroll.refresh();}, 0);
}
}
xmlhttp.open("GET","home_more.php?id="+id,true);
xmlhttp.send();
}