Documentation

editor.getSelectionElement()

Returns the element that contains the current selection. Depending on what is selected, that is the selected control, the table cell the caret sits in, or the element that encloses a highlighted range. Use it to answer “where is the user right now?” — for example to enable table controls only inside a cell.

Syntax

editor.getSelectionElement()

Parameters

None.

Returns

The enclosing element, or null when the selection has no meaningful single container (for example an empty selection with no anchor).

Behavior notes

  • A selected control wins.If a control (such as an image) is selected, it is returned directly — the same element you would get from getSelectedControl().
  • A caret in a table cell returns that cell. When the selection is collapsed inside a TD or TH, that cell is returned — convenient for enabling cell-aware actions.
  • A range returns its enclosing element. For a highlighted range, getSelectionElement() walks up to the element that contains both ends of the selection.

Example usage

Find out whether the caret is inside a table cell:

var editor1 = new RichTextEditor("#div_editor1");
var el = editor1.getSelectionElement();
var inCell = el && (el.closest("td") || el.closest("th"));

Read the nearest link at the current selection:

var el = editor1.getSelectionElement();
var link = el ? el.closest("a[href]") : null;
if (link) console.log("Editing link:", link.href);

Related