Documentation

editor.getSelectedText()

Returns the plain-text content of the current selection — the text the user has highlighted, with formatting stripped. Use it for word counts, lookups, “search the web for this phrase” actions, or anything that needs the selected text rather than its markup.

Syntax

editor.getSelectedText()

Parameters

None.

Returns

The selected text as a string, or null when the selection is collapsed (a caret with nothing highlighted).

Behavior notes

  • Collapsed selection returns null. With just a caret and no highlighted text, getSelectedText() returns null— check for that before using the result.
  • Plain text, not HTML. The result is the text only; use getHTMLCode() or the selection range if you need the marked-up content.
  • Reflects the live selection,so read it at the moment you need it (for example inside a toolbar button’s click handler).

Example usage

Act on the highlighted text, guarding against an empty selection:

var editor1 = new RichTextEditor("#div_editor1");
var text = editor1.getSelectedText();
if (text) {
    window.open("https://www.google.com/search?q=" + encodeURIComponent(text));
}

Show a live character count of the current selection:

var text = editor1.getSelectedText() || "";
console.log(text.length + " characters selected");

Related