Link to home
Start Free TrialLog in
Avatar of cophi
cophi

asked on

AJAX responseText

The response text that I have coming back is a complete HTML page.
Is there any way to set that response text to a document... and get the innerHTML of an element by ID???

So right now I have on my callback javascript

document.getElementById("ajax-div").innerHTML = http_request.responseText;

I would like it to be something like...

document.getElementById("ajax-div").innerHTML = http_request.responseText.getElementById("html-for-ajax").innerHTML;
Avatar of Morcalavin
Morcalavin
Flag of United States of America image

responseText is a string object, and unfortunately I do not believe it is possible to do as you propose.
responseXML returs an xml stream object, which has methods similar to what you want, but the response has to be in XML format.  I've never tried using it on something that is in HTML format.  You may want to try it just to see what will happen.

Your alternative would be to change the return of the ajax request to only return the html you want, or to parse the entire html to retrieve the data you want.
Something like this:
text = responseText.value.match(/<div id=\"yourelementid\">(.*?)<\/div>/);
document.getElementById("ajax-div").innerHTML = text[0];
document.getElementById("ajax-div").innerHTML = text[0];
This should be:
document.getElementById("ajax-div").innerHTML = text[1];
Avatar of cophi
cophi

ASKER

>>responseText.value.match(/<div id=\"yourelementid\">(.*?)<\/div>/);

Would that work if I have other div's inside this div??

>>Your alternative would be to change the return of the ajax request to only return the html you want, or to parse the entire html to retrieve the data you want.

I tried this method in my ASP.Net Application, but I don't know of a good way to parse it.  Do you?
ASKER CERTIFIED SOLUTION
Avatar of Morcalavin
Morcalavin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial