Contents
- Select and get the word in current cursor position
- Select and get the paragraph in current cursor position
Having trouble getting help?
Contact Support
Contact Support
How to select and retrieve the word and paragraph in current cursor position in Document Editor component
13 May 20252 minutes to read
You can get the current word or paragraph content from the Document 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 Document 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 Document 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>