JavaScript Helper Classes: Converting Selected Text to Form Fields
This quick tutorial shows how to implement JavaScript helper classes for typical tasks. In this sample, a function is implemented that converts selected text in a document into a text form field with a single function call.

The online document editor of TX Text Control available for Angular, ASP.NET MVC, ASP.NET Core and very soon as a React component (!) shares the same underlying Java
It can be used to insert text, replace text in merge fields or to insert form fields programmatically. The following sample code inserts a Text
TXTextControl.formFields.getCanAdd(canAdd => {
if (canAdd) {
// Add form field
TXTextControl.formFields.addTextFormField(emptyWidth);
} else {
console.log("error");
}
});
Sometimes, instead of adding this code directly to your functions, it is useful to create helper classes to encapsulate typical tasks in reusable components. The following helper class TXHelper provides the function textFormFields.convertFromSelection that converts the currently selected text into a form field:
var TXHelper = (function (tx) {
tx.textFormFields = {
convertFromSelection: function (name, id) {
// get current selection
var sel = TXTextControl.selection;
sel.getLength(function(selLength) {
if (selLength > 0) { // text is selected
sel.getText(function(selText) {
// store text
var text = selText;
sel.setText(""); // remove selected text
TXTextControl.formFields.getCanAdd(canAdd => {
if (canAdd) { // can add the field here?
// add form field
TXTextControl.formFields.addTextFormField(600, ff => {
ff.setText(text);
// set field properties
if (name) ff.setName(name);
if (id) ff.setID(id);
});
} else { // there is a field at that location
console.log("A field cannot be added at this location.");
}
});
});
}
else { // no text is selected
console.log("Nothing is selected.");
}
});
},
};
return tx;
} ( TXHelper || {} ));
This helper function can be called like shown in the following code. The text of the current selection is used to add a form field at the current input position. Before that, the text of the selection is removed. Optionally, you can pass a Name and ID for the new form field:
TXHelper.textFormFields.convertFromSelection("name", 5);
ASP.NET
Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
JavaScript: Avoid Flickering and Visual Updates by Grouping Undo Steps
The JavaScript API can be used to group several steps into one undo action that can be undone with a single undo call. Additionally, those groups are visually updated at once to avoid screen…
DS Server or TX Text Control? Different Deployment Scenarios
The Text Control document processing technology can be deployed in various ways using different products for different applications. This overview explains the differences and deployment strategies.
Document Editor: JavaScript Object Availability and Order of Events
The online document editor, available for MVC and Angular, provides a JavaScript API to manipulate the document and the style and behavior of the editor. This article explains the order of events…
Using TX Text Control .NET Server in Blazor Server Apps
The TX Text Control document editor and the server-side non-UI component ServerTextControl can be used in Blazor Server Apps. This article shows how to save documents to the server and how to…
Using DS Server with Blazor
This sample and article shows how to integrate the DS Server DocumentEditor using JavaScript into an ASP.NET Blazor application. Blazor allows you create interactive web applications using C#.