Get specific content off responseText
Asked Answered
E

3

6

I am making an ajax call and I get the content from responseText.

I confirm by using alert that inside, the responseText contains the entire page as a string.

That's fine. Now I need to extract specific contents from the string by converting it to DOM and using getElementsByTagName, getElementsByName etc...

My problem is that none of these seems to work.

I've read many references on using responseXML instead of responseText but this gives me back null.

So, by sticking to responseText, how can i get the specific elements that I want?

For instance if my response contains the following:

<html>
<head>....</head>
<body>....
 .....
 <table>
 <tr>
 <td>sometext</td>
 <td>someothertext</td>
 </tr>
 <tr>
 <td>sometext</td>
 <td>someothertext</td>
 </tr>
 <tr>
 <td>sometext</td>
 <td>someothertext</td>
 </tr>    
 </table> 
 <div> somediv </div>
 </body>
 </html>     

how can I access the second row of the table and its value? Or the div etc.. so, now in my actual html, i have sth like this

<html>
<head>.....</head>
<body>
<table><tr><td id="test">Test</td></tr></table>
</body>
</html>

i want the extracted element/value to be parsed inside the table of my actual html..

document.getElementById('test').innerHTML = the_extracted_elements_from_responseText

Engrossing answered 19/11, 2012 at 19:29 Comment(0)
T
13

You need to use a DOMParser on the html and use DOM traversal methods on the resulting document to return the desired node(s).

  var parser=new DOMParser();
  var xmlDoc=parser.parseFromString(responseText,"text/xml");
  var tds = xmlDoc.getElementsByTagName("td");

Note that IE would require new ActiveXObject("Microsoft.XMLDOM"); instead.

Taiga answered 19/11, 2012 at 19:34 Comment(5)
about Note that IE would require new ActiveXObject("Microsoft.XMLDOM"); instead. is this on newer browsers as well..?? cause i thought that this was only necessary on i.e7 and earlier..Engrossing
@Engrossing I can't find a reference for that but yes off the top of my head IE8+ have DOMParserTaiga
i have used document.getElementById('test').innerHTML in my code, and now i get [object Nodelist] shouldn't it just parse the entire td to the specified area?Engrossing
@Engrossing It isn't entirely clear what you're asking. Could you please elaborate?Taiga
i edited my original post by adding the next part i m talking about..since i m able now to handle some content out of the responseText, how can this content be parsed into my main html file..?Engrossing
G
0

create the variable array and make the push.

const result = [];
for(let i = 1; i < ?? ; i++) {
const test = document.getElementById('test').innerHTML;
result.push({ test })
}
return { result }
})
Gasbag answered 2/8, 2020 at 2:50 Comment(0)
S
0

try this:

var temp = document.createElement('div');
temp.innerHTML = xhr.responseText;//xhr is your own xhr obj
//select the element u want, such as the follow one
var foo = temp.querySelector('#foo');
Schnitzel answered 18/6, 2021 at 6:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.