Manipulating the Context Menu in the Document Editor using JavaScript
This article shows how to manipulate the context menu in the Document Editor in JavaScript. The context menu can be customized by adding or removing items or by changing the event handler of existing items.

A fully customizable shortcut menu is available in the Document Editor since version 31.0 of TX Text Control .NET Server. A new contextMenuOpening event, which allows you to change the items before the menu is rendered, can be used to access the contents of the context menu.
Context
The items property returns all available items that can be manipulated.
In this article we will discuss 4 different typical scenarios when you manipulate the context menus:
- Removing an existing item from the context menu.
- Removing a sub-item from the context menu.
- Disabling an existing item.
- Changing the click handler of an existing item.
Based on the standard context menu for inserting a footnote (Insert -> Footnote), the examples should show the scenarios that are typical for this.
Removing Items
Removing an existing item from the context menu is very simple. The following code removes the Insert item from the context menu:
// remove the "Insert" menu item
TXTextControl.addEventListener("contextMenuOpening", e => {
// check if e.items contains an entry where property text == "Insert"
const index = e.items.findIndex(i => i.text === "Insert");
// if so, remove it
if (index > -1) {
e.items.splice(index, 1);
}
})
Removing Sub-Items
Removing a sub-item from the context menu is a little bit more complex. The following code removes the Footnote item from the Insert item:
// remove the "Footnote" menu item
TXTextControl.addEventListener("contextMenuOpening", (e) => {
// check if e.items contains an entry where property text == "Insert"
const index = e.items.findIndex((i) => i.text === "Insert");
// check if e.items[index].items contains an entry where property text == "Footnote"
const index2 = e.items[index].items.findIndex((i) => i.text === "Footnote");
// if so, remove it
if (index2 > -1) {
e.items[index].items.splice(index2, 1);
}
})
First, it searches for the Insert menu item, and then it searches for the Footnote item in its item collection. If found, the item is removed from the array.
Disabling the Item
Disabling an existing item is also very simple. The following code disables the Footnote item:
// disable the "Footnote" menu item
TXTextControl.addEventListener("contextMenuOpening", (e) => {
// check if e.items contains an entry where property text == "Insert"
const index = e.items.findIndex((i) => i.text === "Insert");
// check if e.items[index].items contains an entry where property text == "Footnote"
const index2 = e.items[index].items.findIndex((i) => i.text === "Footnote");
// if so, disable it
e.items[index].items[index2].isEnabled = false;
})
First, it searches for the Footnote item and then it sets the isEnabled property to false.
Changing the Click Handler
The click handler can also be subject to manipulation. This sample code overrides the default action and adds an alert to the function.
// cancel the click handler of the "Footnote" menu item
TXTextControl.addEventListener("contextMenuOpening", (e) => {
// check if e.items contains an entry where property text == "Insert"
const index = e.items.findIndex((i) => i.text === "Insert");
// check if e.items[index].items contains an entry where property text == "Footnote"
const index2 = e.items[index].items.findIndex((i) => i.text === "Footnote");
// if so, cancel the click handler
e.items[index].items[index2].clickHandler = function(e) {
e.cancel = true; alert("Footnote is disabled");
}
})
Related Posts
Document Editor: Initialization Events
There are several events that the TX Text Control provides during the initialization phase. These events and the order in which they are fired are explained in this article.
ASP.NETJavaScriptDocument Editor
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…
ASP.NETASP.NET CoreDocument Editor
Getting Started Video Tutorial: Document Editor in ASP.NET Core C# on Linux
This video tutorial shows how to use the Document Editor in an ASP.NET Core application using C# and deploy on Linux using Docker. This tutorial is part of the TX Text Control Getting Started…
ASP.NETDocument EditorDocument Protection
Document Protection in ASP.NET with TX Text Control: Healthcare Use Cases
This article explores document protection use cases in the healthcare domain. It also explains how to assign usernames, configure edit modes, and use editable regions based on user roles using the…
ASP.NETApp ServicesASP.NET Core
Deploying the TX Text Control Document Editor in an ASP.NET Core Web App to…
This tutorial shows how to deploy the TX Text Control Document Editor to Azure App Services using an ASP.NET Core Web App. The Document Editor is a powerful word processing component that can be…