Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
113
rated 0 times [  117] [ 4]  / answers: 1 / hits: 15720  / 13 Years ago, sun, september 25, 2011, 12:00:00

I want to insert html at the current range (a W3C Range).



I guess i have to use the method insertNode. And it works great with text.



Example:



var node = document.createTextNode(some text);
range.insertNode(node);


The problem is that i want to insert html (might be something like <h1>test</h1>some more text). And there is no createHTMLNode().



I've tried to use createElement('div'), give it an id, and the html as innerHTML and then trying to replace it with it's nodeValue after inserting it but it gives me DOM Errors.



Is there a way to do this without getting an extra html-element around the html i want to insert?


More From » dom

 Answers
11

Because <h1>test</h1>some more text consists of an HTML element and two pieces of text. It isn't a node.



If you want to insert HTML then use innerHTML.




Is there a way to do this without getting an extra html-element around the html i want to insert?




Create an element (don't add it to the document). Set its innerHTML. Then move all its child nodes by looping over foo.childNodes.


[#89938] Thursday, September 22, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kareem

Total Points: 733
Total Questions: 110
Total Answers: 102

Location: Bermuda
Member since Thu, Apr 20, 2023
1 Year ago
;