[Project_owners] How to insert text as HTML?

Michael Nachbaur mike at nachbaur.com
Tue Aug 30 17:21:04 EDT 2005


Michael Vincent wrote:
> Eric Jung wrote:
> 
>> Use the DOM Core Level 1 API
>> http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core.html
>>
>> var myDiv = document.getElementById("mydiv");
>> var link = document.createElement("a");
>> var linkText = document.createTextNode("MultiZilla");
>> link .setAttribute("href", "http://multizilla.mozdev.org/");
>> link .appendChild(linkText);
>> myDiv.appendChild(link);
> 
> 
> Thanks Eric, but I'm afraid that my HTML example snippet was not clear 
> enough because what I need really is to insert that link inside a <p> 
> element, at a given position. Lets take this example:
> 
> <p>Here's a better test</p>
> 
> and the text 'better' should become my link, so how does that work?


You'll need to remove the text node that is the child of the "p" tag, 
and create two new text nodes:  one for the text "Here's a ", and the 
second for " test".  I have some code lying around somewhere that does 
this, but I just got back from vacation and have no clue where I put 
anything.  This might get you on the right track though:

http://xulplanet.com/references/objref/Text.html#method_splitText


More information about the Project_owners mailing list