Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AJAX innerHTML update, then reference the new innerHTML

Status
Not open for further replies.

XgrinderX

Programmer
Mar 27, 2001
225
US
thread1600-1279426

I hope the thread reference above works. I tried both methods and could not get either to work, but I may have misunderstood.

I am trying to use AJAX to load some new form fields into the page. Once those fields are loaded, I need to initialize and load some values from those fields into arrays. It seems no matter what I do, the init and load code runs before the form fields are added and I get errors.

Sample Code:
Code:
function generateFields() {
	var xmlhttp = createXMLHttpRequest();
	if (xmlhttp) {
		xmlhttp.onreadystatechange = function () {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				document.getElementById("div_id").innerHTML = xmlhttp.responseText;
				initializeArrays();
				loadValues();
			} else {
				document.getElementById("div_id").innerHTML = "<img src=\"loading.gif\" />";
			}
		}
		strURL = "ajax_handler.asp";
		xmlhttp.open("GET", strURL, true);
		xmlhttp.send(null);
	}
}

In the sample above, initializeArrays() and loadValues() end up being executed before div_id is updated. How can I prevent this?

From the reference thread, I tried changing the open parm to false and that did not work. I also tried setting up a new function called loadArrays that called the init and load values functions and then passing it in as mentioned in that thread and that did not work for me either. What am I doing wrong?

-Greg
 
no matter what I do, the init and load code runs before the form fields are added and I get errors.

Why not try sending only the values back in the response (whether in XML or some sort of delimited string (e.g. 'a|b|c') instead of the form fields?

That way you know you have the values, and can generate the form fields without having to wait for the DOM to update.

Or, perhaps put a small timer (say 250 ms) in place after the successful response... just enough to give the DOM time to update before calling your routines. Either of those should work.

Hope this helps,

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top