niche Posted September 7, 2011 Share Posted September 7, 2011 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 More sharing options...
Ingolme Posted September 7, 2011 Share Posted September 7, 2011 Well, if you have an element with an ID "txtHint" then its content should change. <div id="txtHint">Content will appear here</div> Link to comment Share on other sites More sharing options...
JamesB Posted September 7, 2011 Share Posted September 7, 2011 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 More sharing options...
niche Posted September 7, 2011 Author Share Posted September 7, 2011 Thanks Ingolme and JamesB . Your help is always appreciated. Link to comment Share on other sites More sharing options...
skaterdav85 Posted September 7, 2011 Share Posted September 7, 2011 also, an ID isn't an attribute. it is an HTML element property. http://www.w3schools.com/jsref/dom_obj_all.asp i believe you should be doing this to set the ID: div.id = 'txtHint'; Link to comment Share on other sites More sharing options...
thescientist Posted September 7, 2011 Share Posted September 7, 2011 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.