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;
JavaScript

Avatar of undefined
Last Comment
Morcalavin

8/22/2022 - Mon
Morcalavin

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];
Morcalavin

document.getElementById("ajax-div").innerHTML = text[0];
This should be:
document.getElementById("ajax-div").innerHTML = text[1];
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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Morcalavin

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question