Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4321886
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:48:11+00:00 2026-05-21T08:48:11+00:00

I’m doing an Ajax request in which I have the server returning well-formed HTML,

  • 0

I’m doing an Ajax request in which I have the server returning well-formed HTML, specifically table row (“tr”) elements. I had hoped to use the DOM to insert these rows into a table by reading from the “responseXML” member on the request object (XMLHttpRequest). Basically, I have

var myTable = document.getElementById("myTable");
var tableRows = responseXML.getElementsByTagName("tr");
for (var i = 0; i < tableRows.length; i++) {
  myTable.appendChild(tableRows[i]);
}

Lots of things go right here. The “responseXML” contains the correct data, and the call to “getElementsByTagName” also works correctly. Sadly, it breaks down when I try to add a row by calling appendChild (I get a vague javascript error). I think this is because although each item in the “tableRows” array is a correct XML element, the browser can’t automatically interpret it as an HTML element. I’ve noticed that if I make a new using document.createElement(“tr”) and examine it during run-time, it looks different in memory from the items in the “tableRows” array. Also, I am able insert that new (the one made with “createElement”) into the table without an error.

Is there a good way to do what I’m trying to do here? I was hoping that I could make the browser interpret the elements correctly by returning the content as XML and using responseXML/getElementsByTagName. That way, I could avoid using responseText for what is really HTML, and I could also avoid using “innerHTML” because it’s not a standard. Most examples online use innerHTML, however. Is that really the best way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T08:48:12+00:00Added an answer on May 21, 2026 at 8:48 am

    If it is well-formed HTML, you can try using document.importNode() like so:

    //myXHTML is your HTML/XHTML (if it is well-formed, you should be ok).
    //myObject is the parent node. This is the node you want to insert your HTML into
    function insertXHTML(myXHTML, myObject) {
       var parser = new DOMParser();
    
       //Doing this to make sure that there is just one root node.
       //You can change the namespace, or not use it at all.
       var xmlToParse = "<div xmlns = \"http://www.w3.org/1999/xhtml\">" + myXHTML + "</div>";
       var XMLdoc = parser.parseFromString(xmlToParse, "application/xhtml+xml");
    
       var root = XMLdoc.documentElement;
    
       for(i = 0; i < root.childNodes.length; i++) {
           myObject.appendChild(document.importNode(root.childNodes[i], true));
       }
    }
    

    This is code I wrote some time ago (to get around the use of innerHTML); it should still work. The only thing I am worried about is DOMParser(); not sure how cross-browser it is.

    EDIT

    The problem with your approach (doing things with just pure JS) is that you’ll run into cross-browser issues :(. For importNode, take a look at this cross-browser implementation. Your other option is to use something like jQuery. It normalizes a lot of the cross-browser differences.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.