Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
8
rated 0 times [  9] [ 1]  / answers: 1 / hits: 42866  / 13 Years ago, sun, november 20, 2011, 12:00:00

I'm creating a document fragment as follow:



var aWholeHTMLDocument = '<!doctype html> <html><head></head><body><h1>hello world</h1></body></html>';
var frag = document.createDocumentFragment();
frag.innerHTML = aWholeHTMLDocument;


The variable aWholeHTMLDocument contains a long string that is the entire html document of a page, and I want to insert it inside my fragment in order to generate and manipulate the DOM dynamically.



My question is, once I have added that string to frag.innerHTML, shouldn't it load this string and convert it to a DOM object?



After setting innerHTML, shouldn't I have access to the DOM through a property?



I tried frag.childNodes but it doesn't seem to contain anything, and all I want is to just access that newly created DOM.


More From » dom

 Answers
16

You can't set the innerHTML of a document fragment like you would do with a normal node, that's the problem. Adding a standard div and setting the innerHTML of that is the common solution.


[#89010] Friday, November 18, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kinsleyashlynnh

Total Points: 64
Total Questions: 119
Total Answers: 98

Location: Burundi
Member since Sat, Aug 21, 2021
3 Years ago
;