Products Technologies Demo Docs Blog Support Company

JavaScript Functions for Typical Form Field Tasks

The new JavaScript API not only enables programmatic access to all elements of a document, but also provides events for user interaction.

JavaScript Functions for Typical Form Field Tasks

Version X18 (28.0) is shipped with a complete new JavaScript API to program the online document editor that comes with TX Text Control .NET Server.

The complete JavaScript API documentation can be found in our online docs:

JavaScript API

This article describes typical tasks for inserting and processing form fields such as check boxes, drop-down boxes and form text fields.

Adding Form Text Fields

Form text fields can be used to retrieve textual content from users. A form text field has a default empty width (first parameter in the addTextFormField method) and it is auto-extending.

function addTextFormField() {
        TXTextControl.formFields.getCanAdd(canAdd => {
                if (canAdd) {
                        TXTextControl.formFields.addTextFormField(0, function(insertedFormField) {
                                insertedFormField.setName("Field1");
                        });
                } else {
                        alert("Form field cannot be added at this location.");
                }
        });
}

The getCanAdd method should be used for all fields inherited from TextField. It returns true, if the field can be added at the current input position. A text field (or form fields) cannot be nested and therefore they cannot be inserted at positions that already contain a field. The addtextformfield method returns the field itself in the callback and can be directly used to set additional properties such as the name property that is defined using the setName method.

Get Form Field By Name

While the getItem method returns a field at the current input position or based on a given ID, the forEach method can be used to iterate through all elements of form field collections.

The following asynchronous function loops through all form fields and checks for the name property. If found, the Promise is resolved by returning the form field itself.

function getFormFieldByName(name) {
        return new Promise(resolve => {
                TXTextControl.formFields.forEach(function(formField) {
                        formField.getName(function(formFieldName){
                                if (formFieldName === name)
                                {
                                        resolve(formField);
                                }
                        })
                })
        });
}

var formField = await getFormFieldByName("Field1");

This function needs to be called from an async function with the await operator to wait for the response.

Inserting Check Form Fields

Analogically with form text fields, check boxes can be added using the addCheckFormField method.

function addCheckFormField() {
        TXTextControl.formFields.getCanAdd(canAdd => {
                if (canAdd) {
                        TXTextControl.formFields.addCheckFormField(true);
                }
        });
}

Getting Form Fields at the Input Position

In order to get the field at the current input position, the getItem method can be used. The following code snippet shows how to change the state of a check form field when the field retrieves the input position. Usually, a check box is first activated and then checked and therefore requires two clicks initially to change the state of the check box.

function textFieldEnteredHandler(enteredField) {

    if (enteredField.textField.type === "CHECKFORMFIELD")
    {
        TXTextControl.formFields.getItem(function(checkFormField) {
            checkFormField.getChecked(function(checked) {
                checkFormField.setChecked(!checked);
            })
        })
    }
}

TXTextControl.addEventListener("textFieldEntered", textFieldEnteredHandler);

Adding Selection Form Fields

The following code shows how to insert a non-editable selection form field with pre-defined items. The setSelectedIndex method pre-selects a specific item in the list of items.

function addDropDownFormField(items) {

        TXTextControl.formFields.getCanAdd(canAdd => {
                if (canAdd) {

                        TXTextControl.formFields.addSelectionFormField(0, sff => {
                                
                                // no custom text
                                sff.setEditable(false);
                                sff.setItems(items);

                                // select "Uber"
                                sff.setSelectedIndex(3);
                        });
                }
        });
}

var transportation = ["Bicycle", "Car", "Scooter", "Uber", "Cab"];

addDropDownFormField(transportation);

Test this on your own using all document editor versions including Angular, Node.js, MVC and Web Forms that all share the same JavaScript API.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • Javascript: Collection.forEach Method
  • Javascript: FormFieldCollection.addCheckFormField Method
  • Javascript: FormFieldCollection.addTextFormField Method
  • Javascript: FormFieldCollection.getItem Method
  • Javascript: SelectionFormField.setSelectedIndex Method
  • Javascript: TextField Object
  • Javascript: TextField.setName Method
  • Javascript: TextFieldCollectionBase.getCanAdd Method
  • Javascript: TXTextControl.TextField.name Property

Angular

Integrate document processing, editing, sharing, collaboration, creation, electronic signatures, and PDF generation into your Angular Web applications.

Learn more about Angular

Related Posts

AngularASP.NETJavaScript

Observe When the Reporting Preview Tab is Active Using MutationObserver

This article shows how to observe when the Reporting Preview tab is active using MutationObserver. The Reporting Preview tab is a feature of the TX Text Control Document Editor that allows you to…


AngularASP.NETJavaScript

Building an ASP.NET Core Backend Application to Host the Document Editor and…

This article explains how to create an ASP.NET Core backend application to host the Document Editor and Document Viewer. This backend application is required to provide the required functionality…


AngularASP.NETBlazor

Building an ASP.NET Core Backend (Linux and Windows) for the Document Editor…

This article shows how to create a backend for the Document Editor and Viewer using ASP.NET Core. The backend can be hosted on Windows and Linux and can be used in Blazor, Angular, JavaScript, and…


AngularJavaScriptDocument Editor

Getting Started: Document Editor Version 33.0 with Angular CLI 19.0

This article shows how to use the TX Text Control Document Editor version 33.0 npm package for Angular within an Angular CLI 19.0 application. It uses the trial backend running on our servers, but…


AngularASP.NETJavaScript

Using the Document Editor in SPA Applications using the removeFromDom Method

This article shows how to use the removeFromDom method to remove the Document Editor from the DOM when it is no longer needed. This is useful when the Document Editor is used in a Single Page…