How to Get Current Word in ASP.NET MVC DOCX Editor
27 Aug 20262 minutes to read
You can get the current word or paragraph content from the DOCX Editor component as plain text and SFDT (rich text).
Select and get the word in current cursor position
You can use [selectCurrentWord] API in selection module to select the current word at cursor position and use [text] API to get the selected content as plain text from DOCX Editor component.
The following example code illustrates how to select and get the current word as plain text.
@Html.EJS().DocumentEditorContainer("container").Created("onCreated").EnableToolbar(true).Render()
<script>
var documenteditor;
var container;
function onCreated() {
var documenteditorElement = document.getElementById("container");
container = documenteditorElement.ej2_instances[0];
// To insert text in cursor position
container.documentEditor.editor.insertText('Document editor');
// To select the current word in document
container.documentEditor.selection.selectCurrentWord();
// To get the selected content as text
var selectedContentText = container.documentEditor.selection.text;
// To get the selected content as SFDT (rich text)
var selectedContentSFDT = container.documentEditor.selection.sfdt;
}
</script>Select and get the paragraph in current cursor position
You can use [selectParagraph] API in selection module to select the current paragraph at cursor position and use [text] API or [sfdt] API to get the selected content as plain text or SFDT from DOCX Editor component.
The following example code illustrates how to select and get the current paragraph as SFDT.
@Html.EJS().DocumentEditorContainer("container").Created("onCreated").EnableToolbar(true).Render()
<script>
var documenteditor;
var container;
function onCreated() {
var documenteditorElement = document.getElementById("container");
container = documenteditorElement.ej2_instances[0];
// To insert text in cursor position
container.documentEditor.editor.insertText('Document editor');
// To select the current paragraph in document
container.documentEditor.selection.selectParagraph();
// To get the selected content as text
var selectedContentText = container.documentEditor.selection.text;
// To get the selected content as SFDT (rich text)
var selectedContentSFDT = container.documentEditor.selection.sfdt;
}
</script>