views:

944

answers:

2

What is the best way to get the full String representation of an HTMLCollection object in javascript/jquery? The output String should have an XML syntax.

+1  A: 
$('<div/>').append(HTMLCollection).html(); // Retrieves the "outerHTML"
J-P
Thank you for your quick answer but it didn't work for me. I get a TypeError: this[0].innerHTML is undefined. This object is set though, as it has a length of 66. Apparently the HTMLCollection isn't in HTML but in XML, as it actually is a SOAP response from a web service. Any ideas on how to get a string out of this?
David
Given what you've said, the only reliable way of doing this is to iterate through each node and build up a string. May I ask what purpose this string is to serve?
J-P
Maybe this will help: http://www.adahas.com/2005/05/innerhtml-in-applicationxhtmlxml/
J-P
I am actually creating a javascript app that has to modify a SOAP response from a SharePoint web service and use it call another one. That call is made though an API (SPAPI) that requires strings as parameters. I have found a way build my string using firefox, but it doesn't seems to work with IE. Here it is:var xmlDoc = wsResponse.responseXML;var xmlFields = xmlDoc.getElementsByTagName("FieldInformation");var xml = new XMLSerializer();var string = "";for(var i=0; i<xmlFields.length; i++){ string = string + xml.serializeToString($(xmlFields)[i]);}
David
A: 

My guess is to first clone the nodes before trying the div trick:

$('<div/>').append($(HTMLCollection).clone()).html();

Without cloning, the reassignment of the elements of the collection to the newly created div will fail.

Cheers,

Boldewyn