Jump to content

Get Ouput Inside A Div - Solved With Thanks In The Final Post


niche

Recommended Posts

I thought I knew how, but I'm unclear how to get this function to display: "document.getElementById("txtHint").innerHTML=xmlhttp.responseText;" inside the div. Will you show me please?

xmlhttp.onreadystatechange=function() {	if (xmlhttp.readyState==4 && xmlhttp.status==200) {	  var div = document.createElement('div');	  div.setAttribute('id', 'txtHint');	  div.setAttribute("style","background-color:red;");	  div.style.width = '300px';	  div.style.height = '100px';	  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;	  //var txt='hello world!';	  document.getElementsByTagName('body')[0].appendChild(div);	  document.getElementById('mydiv2').innerHTML=txt;	}  } 

Link to comment
Share on other sites

document.getElementById("txtHint") won't be able to fetch the element until you've added it to the document.in your code, the appendChild() call adds the element to the document. you could put the document.getElementById("txtHint") line after the appendChild() line, or simply change it to:

div.innerHTML=xmlhttp.responseText;

Link to comment
Share on other sites

For all the work you are doing just to create the element (unless it's for practice) it might just be more practical to create a class for the element (to style it ahead of time) and give it an initial display property of none, and then just set it to block after you have updated the innerHTML.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...