cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

converting a DocumentFragment to a markup string

jsulak
1-Newbie

converting a DocumentFragment to a markup string

Hello Adepters,



Can I convert a DOM DocumentFragment into a markup string? I'm looking
for an equivalent to ARange.toMarkupString() on a DocumentFragment
object, but it doesn't seem to exist. Alternatively, can I convert a
DocumentFragment to an ARange?



About a year ago I asked a related question, and Clay suggested using
the document.createRange() and range.selectNode() methods to convert a
DocumentFragment into a ARange, but I can't get it to work:







1 REPLY 1

Hi James-



There appears to be some wonkiness in how range.selectNodeContents() works
with document fragments. (Or maybe the wonkiness is in my interpretation of
the DOM spec...) But I did find a construction that seems to work. Try
something like this:



// assume docfrag contains Document Fragment object of interest

range2 = docfrag.ownerDocument.createRange();

range2.setStart(docfrag.firstChild,0);

range2.setEnd(docfrag.lastChild,1);

fragmarkup = range2.toMarkupString();



HTH.



--Clay



Clay Helberg

Senior Consultant

TerraXML


Announcements