Detect Toggle Button Changes Using a MutationObserver
This article shows how to detect changes of toggle buttons in the ribbon of the web editor using a MutationObserver. The state of a toggle button in the ribbon visualizes the state of a certain property. For example a toggled list button shows, that the current input position belongs to a list.

In order to react to a toggle button change, add an event listener to the ribbonTabsLoaded event and identify the id of the desired ribbon element for example by using the browser developer tool.

MutationObserver
A MutationObserver will allow you to react to changes in the DOM element. Switch statements can be used to distinguish between different cases.
It is important to use the correct observer configuration. Make sure to observe changes in the whole sub tree and also to pass the right root element as a target node in the MutationObserver.observe() method.
This code snippet shows how to detect toggles of the Bullet List dropdown in the ribbon Home tab. Therefore, the Paragraph group is passed as root element.
window.addEventListener("load", () => {
TXTextControl.addEventListener("ribbonTabsLoaded", () => {
// Obtain "Paragraph" ribbon group element (get further ribbon group element ids
// by using the browser developer tools)
const ribbonGroupParagraph = document.getElementById("ribbonGroupParagraph");
const observer = new MutationObserver(mutations => {
for (mutation of mutations) {
switch (mutation.type) {
case "attributes":
switch (mutation.attributeName) {
case "class":
// The "class" attribute of an element has been changed.
// Get the affected element and figure out which button
// was pressed via the element id.
const button = mutation.target;
switch (button.id) {
case "ribbonTabHome_drpDnBtnBulletList":
// "Bulleted list" button.
// The "toggled" state can be determined via the CSS class
// "ribbon-button-selected".
if (button.classList.contains("ribbon-button-selected")) {
console.info("'Bulleted list' button toggled.")
}
else {
console.info("'Bulleted list' button un-toggled.")
}
break;
case "ribbonTabHome_drpDnBtnNumberedList":
// "Numbered list" button.
// ...
break;
// Get further button ids by browsing the html structure
// using the browser developer tools.
}
break;
}
break;
}
}
});
// Observer configuration (We want to observe changes in the whole sub-tree of the
// "Paragraph" group root element).
const config = { attributes: true, subtree: true };
// Pass in the target node (in this case the "Paragraph" group root element) and
// the observer options.
observer.observe(ribbonGroupParagraph, config);
});
});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
Implementing Conditional Table Cell Colors with MailMerge
This ASP.NET MVC sample shows how to implement conditional table cell colors using the online document editor and an ASP.NET backend.
Best Practices for Adding Ribbon Tabs, Groups and Buttons to the…
This article explains how to add ribbon content such as additional ribbon tabs, groups and buttons to the TXTextControl.Web ribbon bar.
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…
ASP.NETJavaScriptDocument Editor
Removing Empty Pages in TX Text Control with JavaScript
TX Text Control offers a C# solution for removing empty pages already. However, implementing this in JavaScript provides more flexibility for web environments. By using JavaScript methods 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…
