Products Technologies Demo Docs Blog Support Company

Implementing Text-As-You-Type Auto Correct Replacements using JavaScript

Text-As-You-Type Auto Correct is a popular feature that replaces a specific phrase or character combination with a longer text while typing. This sample shows how to implement this feature using JavaScript.

Implementing Text-As-You-Type Auto Correct Replacements using JavaScript

Text-as-you-type auto correct is a popular feature that replaces a specific phrase or character combination with a longer text while typing. Consider you are repeating specific phrases again and again such as "Thanks for your request". In this case, auto correct would convert the typed characters TY into this phrase while typing text.

AutoCorrect with TX Text Control

Keypress Event

For performance reasons, the JavaScript keypress event is used to store contiguous character codes. If the SPACE key is pressed, the function is trynig to find the typed word in the dictionary of shortcuts. In case the word has been found, the shortcut is selected and replaced with the long text.

// dynamic character array
var charCodes = [];

// shortcut dictionary
var dict = {
    "TY": "Thanks for your request.",
    "AQ": "Let me know if you have additional questions.",
    "MY": "I look forward to meeting you.",
    "GD": "Have a great day.",
    "LONG": "This is another longer text.",
};

document.getElementById("txTemplateDesignerContainer").addEventListener("keypress", (event) => {

    // if SPACE is pressed
    if (event.charCode === 32) {

        // convert array to string to get typed word
        var typedWord = String.fromCharCode(...charCodes);

        // check word in dictionary
        if (dict[typedWord] != undefined) {

            // cancel SPACE for TXTextControl
            event.cancelBubble = true;

            // select shortcut and replace with long text
            TXTextControl.inputPosition.getTextPosition(function (startPos) {
                TXTextControl.select(startPos - typedWord.length, typedWord.length, function () {
                    TXTextControl.selection.setText(dict[typedWord] + " ");
                })
            })

        }

        // reset character array
        charCodes = [];
    } else {
        // add character to array
        charCodes.push(event.charCode);
    }

}, true);

Exceptions

The shortcut should be only replaced in case the SPACE key is pressed. In other situations such as the ENTER or TAB key, the shortcut should be ignored. The same is valid in case the input position has been changed to another location. In these cases, the character array charCodes of typed characters is emptied again.

TXTextControl.addEventListener("inputPositionChanged", function (position) {

    // if input position is is not coherent, reset character array
    if (position.textPosition < trackedInputPosition - 1 || position.textPosition > trackedInputPosition + 1) {
        charCodes = [];
    }

    // set current input position
    trackedInputPosition = position.textPosition;
})

document.getElementById("txTemplateDesignerContainer").addEventListener("keydown", (event) => {

    // reset character array for all keys including ENTER, TAB and others except SPACE
    if (event.keyCode < 0x30 && event.keyCode != 32) {
        charCodes = [];
    }

}, true);

Live Demo

We implemented this code as part of our online demos. Test this live by launching the demo application.

Auto Text Replacement Demo

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

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.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETASP.NET CoreMarkdown

A Complete Guide to Converting Markdown to PDF in .NET C#

Learn how to convert Markdown to PDF in .NET C# using Text Control's ServerTextControl component. This guide covers setup, conversion process, and customization options for generating high-quality…


ASP.NETASP.NET CoreDocument Creation

Why PDF Creation Belongs at the End of the Business Process

This article discusses why placing PDF creation at the end of the business process is important for ensuring accuracy and efficiency. The most scalable systems delay PDF generation until the…


ASP.NETASP.NET Core

Document Editor with ASP.NET Core and Docker Support with Linux Containers…

This article shows how to create a Document Editor ASP.NET Core application using Docker with Linux containers in Visual Studio 2026 and .NET 10. We will create a simple web application that…


ASP.NETASP.NET Core

Text Control Sponsors & Exhibits at NDC London 2026

We are pleased to announce that Text Control will be sponsoring and exhibiting at NDC London 2026, one of Europe's leading conferences for professional software developers, once again. Join us…


ASP.NETASP.NET Core

Preflighting PDF/UA Documents with TX Text Control in .NET C#

Accessibility is an important aspect of document creation. In this article, we will explore how to preflight PDF/UA documents using TX Text Control in .NET C# applications. We will discuss the…

Summarize this blog post with:

Share on this blog post on: