In a recent post, we already introduced the hidden Javascript interface that is available in version X12. This interface gives you access to the majority of internal Javascript calls that are used in TextControl.Web. This interface is hidden by default, because it is still in beta and undocumented.

In order to enable the hidden interface, the function enableCommands() must be called.

TXTextControl.enableCommands();

The following code inserts a merge field with a specific name into the currently active TextPart (Header, Footer, MainText or TextFrame).

function insertField(name) {
        var mergeField = {
                Name: name,
                Text: "«" + name + "»"
        };
        TXTextControl.sendCommand(
                TXTextControl.Command.InsertItem,
                TXTextControl.ItemType.FieldAdapter,
                TXTextControl.FieldType.MergeField,
                0, mergeField);
}

The following call inserts a MergeField with the name fieldname at the current input position:

TXTextControl.enableCommands();
insertField('fieldname');

Stay tuned to learn more about the hidden JS interface.