Programmer's Guide > Programming and Scripting Techniques > Basic Document Manipulation Using the DOM and AOM > Selecting, Copying, Moving Content > Inserting Markup at the Caret
  
Inserting Markup at the Caret
The ARange extension includes the method insertParsedString. This method makes it easy to insert strings containing markup (tags and entity references) into a range, including the one that represents the document caret position. The following two examples are equivalent and insert the string “an emphasized word” with the second word “emphasized” enclosed in <emphasis> tags. The first example is implemented using standard DOM methods:
var doc = Application.activeDocument;
var caret = doc.insertionPoint;
var node = caret.endContainer;
var parent = node.parentNode;
// does not consider caret offset into text node
parent.insertBefore(doc.createTextNode("an "), node);
var emph = doc.createElement("emphasis");
emph.appendChild(doc.createTextNode("emphasized"));
parent.insertBefore(emph, node);
parent.insertBefore(doc.createTextNode(" word"), node);
The following example uses the ARange.insertParsedString method:
var doc = Application.activeDocument;
doc.insertionPoint.insertParsedString("an <emphasis>emphasized</> word");