TXTextControl.Web: Creating a Custom ButtonBar using the InputFormat Class
TX Text Control X18 implements the InputFormat class in the JavaScript API that enables developers to build custom button bars.

TX Text Control .NET Server X18 implements the new class Input
Therefore, a simple Bootstrap toolbar is used with Font Awesome icons:
<div id="txButtonBar" class="btn-toolbar" role="toolbar" aria-label="Text Control Button Bar">
<div class="btn-group mr-2" role="group" aria-label="First group">
<button id="txBtn_Bold" type="button" class="btn btn-secondary"><i class="fas fa-bold"></i></button>
<button id="txBtn_Italic" type="button" class="btn btn-secondary"><i class="fas fa-italic"></i></button>
<button id="txBtn_Underline" type="button" class="btn btn-secondary"><i class="fas fa-underline"></i></button>
<button id="txBtn_Superscript" type="button" class="btn btn-secondary"><i class="fas fa-superscript"></i></button>
<button id="txBtn_Subscript" type="button" class="btn btn-secondary"><i class="fas fa-subscript"></i></button>
</div>
<div class="btn-group mr-2" role="group" aria-label="Second group">
<button id="txBtn_LeftAligned" type="button" class="btn btn-secondary"><i class="fas fa-align-left"></i></button>
<button id="txBtn_Centered" type="button" class="btn btn-secondary"><i class="fas fa-align-center"></i></button>
<button id="txBtn_RightAligned" type="button" class="btn btn-secondary"><i class="fas fa-align-right"></i></button>
<button id="txBtn_Justified" type="button" class="btn btn-secondary"><i class="fas fa-align-justify"></i></button>
</div>
</div>
In this sample, the naming of the button id values is very important as they match exactly the member names in the InputFormat class. Let's take a closer look at the Bold button for instance:
<button id="txBtn_Bold" type="button" class="btn btn-secondary"><i class="fas fa-bold"></i></button>
The id is txBtn_Bold. The string part after the hyphen matches the name of the InputFormat get/set method name. In this case, it is the get
The following JavaScript code loops through all buttons in the custom toolbar in order to extract the button feature name from the id attribute. A click event handler is attached to each button that sets the new input format. Additionally, the input
TXTextControl.addEventListener("textControlLoaded", function () {
// loop through all buttons in the "txButtonbar"
$("#txButtonBar button").each(function () {
// get the button functionality from the id attribute
var btnFeature = $(this).attr("id").split("_")[1];
// attach a click event to set the input format
$(this).click(function () { setInputFormat(btnFeature); });
// assemble the associated event name
var eventName = btnFeature.charAt(0).toLowerCase() + btnFeature.slice(1) + "Changed";
// attach the event and set the button state in the event callback
TXTextControl.inputFormat.addEventListener(eventName, function (e) {
setBtnActiveState($("#txBtn_" + btnFeature), e.newValue);
});
initialInputFormat($("#txBtn_" + btnFeature), btnFeature);
});
});
The setInputFormat function is calling the associated set method for the specific input format:
function setInputFormat(format) {
TXTextControl.inputFormat["get" + format](function (boolValue) {
TXTextControl.inputFormat["set" + format](!boolValue);
});
}
Thanks to this very generic JavaScript function, events and functionality is attached to all buttons automatically.
Feel free to download the project from our GitHub repository and try this on your own.
Also See
This post references the following in the documentation:
- Javascript: Input
Format.add Event Listener Method - Javascript: Input
Format.get Bold Method - Javascript: Input
Format Object
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
Requirements for this sample
- Visual Studio 2019
- TX Text Control .NET Server X18 (trial sufficient)
Angular
Integrate document processing, editing, sharing, collaboration, creation, electronic signatures, and PDF generation into your Angular Web applications.
Related Posts
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…
New JavaScript API Calls for Typical MailMerge Tasks
This article shows how to use the improved JavaScript API for typical MailMerge tasks such as inserting merge blocks.
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…
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…
Reuse Angular Document Editor Instances in Bootstrap Tabs
The initialization time of document editor instances is time-consuming and requires resources on both the server side and the client side. To reuse instances, they can be moved within the DOM.…